I have the following code:
interface ObjectWithLorem {
lorem: string;
}
interface ObjectWithIpsum {
ipsum: string;
}
type KeyName = 'lorem' | 'ipsum';
const key: KeyName = 'lorem';
type Object = key === 'lorem' ? ObjectWithLorem : ObjectWithIpsum;
const object: Object = {
lorem: 'Lorem'
}
This is obviously not working, but I was wondering if there was either a way to make it work as-is, or at least an alternative solution to have the same effect - essentially the Object type should either be an ObjectWithLorem or ObjectWithIpsum interface, depending on whether the key variable is lorem or not.
ObjectWithLorem | ObjectWithIpsum(one or the other), but to use it you need to narrow the type to know which property to access. Can you show more of how you want to use this?