When dealing with an enum and wanting to use its values as keys in objects, the type declaration looks like this:
enum Bar {
A, B
}
let dictionary: BarDictType = {
[Bar.A]: "foo",
[Bar.B]: "bar"
}
type BarDictType = {
[key in Bar]: string
}
The above code works without any issues. But when attempting the same approach using an interface:
interface BarDictInterface {
[key in Bar]: string
}
Error messages pop up:
TS1169: An interface's computed property name must refer to a literal type or unique symbol type.
TS2464: Computed property names for interfaces are restricted to 'string', 'number', 'symbol', or 'any' types.
TS2304: 'Key' cannot be found.
Why is there a disparity between using interface versus type in this scenario? What limitations, if any, exist for interfaces that led to this design choice?