Consider the following interface and object:
interface Person {
name: string;
lastName?: string;
}
const obj: { [key: string]: Person } = {
a: { name: 'John' },
b: { name: 'Jane', lastName: 'Doe' },
}
This structure allows keys of any string
type for the object. However, there is a desire to retain key inference in a static fashion.
One possible solution involves specifying keys explicitly like so:
type ObjKeys = 'a' | 'b';
const obj: { [key in ObjKeys]: Person } //...
Nevertheless, this method can be considered overly wordy.
Is there a shorter way to declare "here's the type for all values of this object while preserving statically defined keys?"