I am looking to create a type that can accept a string value as a generic argument and use it to define a key on the type. For example:
const foo: MyType<'hello'> = {
hello: "Goodbye", // this key is required
bar: 2
}
I attempted to define it like this:
const type MyType<T extends string> = {
[key: T]: string,
bar: number,
};
However, I encountered an error stating that
An index signature parameter type must be 'string' or 'number'
, even though in this case, it should be a string. Is there a different syntax to address this issue, or is it a current limitation of TypeScript?