Consider the following enum:
enum Numbers {
ONE=1,
TWO=2
}
The following interface definition throws compile-time errors.
interface Config {
[n in Numbers]: string;
}
- A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
- A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
- Cannot find name 'n'.
But if I use a type alias:
type Config = {
[n in Numbers]: string;
}
Or move the mapped type deeper in the definition:
interface Config {
a: {
[n in Numbers]: string;
};
}
It compiles just fine.
So, why a top level property of an interface can not be a mapped type?
PS: On typescript playground using v4.2.3.