Is there a way to define a type and set it as the optional key's type in an interface for t-shirt size props added to an object?
type Size = `xxs` | `xs` | `s` | `m` | `l` | `xl` | `xxl`;
interface Sizes {
[key: Size]: string;
^^^
}
Encountering the error:
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.ts(1337)
Upon researching, I came across this question, and adjusted my code as below:
type Size = `xxs` | `xs` | `s` | `m` | `l` | `xl` | `xxl`;
interface Sizes {
[key in Size]: string;
^^^^^^^^^^^
}
However, faced with subsequent errors:
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)
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
Cannot find name 'key'.ts(2304)
Seeking clarity on my implementation, any insights would be greatly appreciated.