Is it possible to use a union type as a key in an interface? For example, I want to do something like this:
interface IMargin {
[key in 'foo' | 'bar']: boolean;
}
But I'm getting this error:
A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1169)
Is there any way around this?
The use case is turning an array of values into an interface:
const possibleTypes = ['foo', 'bar'];
interface Types {
foo?: boolean;
bar?: boolean;
}
keyof Types
?