I have a generic which its default type is a string:
interface EntityState<typeOfID = string> {
entities: { [ id: typeOfID]: any };
}
I get the error:
An index signature parameter type must be either 'string' or 'number'.(1023)
I also tried the following:
interface EntityState<typeOfID extends string | number> {
entities: { [ id: typeOfID]: any };
}
And it doesn't work. How to solve it?
You can use Record in such a case.
Consider this:
export const enum MyEnumKeys {
Key1 = 'key1',
}
interface EntityState<typeOfID> {
entities: Record<typeOfID, any>;
}
const test: EntityState<MyEnumKeys> = {
entities: {
key1: 1,
anotherKey: 2 // error here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With