Currently, I am working on a challenge where I aim to establish specific keys for an object while allowing TypeScript to infer the types of the values. Here is a simple example:
const fooVals = {
a: null,
b: null,
c: null,
e: null,
}
type TfooVals = typeof fooVals
type JustKeysOfFooVals = { [key in keyof TfooVals]: any};
// TypeScript correctly deduces the types of foo1Vals but does not flag missing key 'e'
const foo1Vals = {
a: 'string',
b: 10,
c: Promise.resolve('string'),
// e: () => { console.log('bar') }
}
// Flags missing key 'e', but sets all types as any
const foo2Vals: JustKeysOfFooVals = {
a: 'string',
b: 10,
c: Promise.resolve('string'),
e: () => { console.log('bar') }
}
Do you think this approach is achievable?