An index signature parameter type cannot be a union type. Consider using a mapped object type instead
Full error message
I'm trying to use the following pattern:
enum Option {
ONE = 'one',
TWO = 'two',
THREE = 'three'
}
interface OptionRequirement {
someBool: boolean;
someString: string;
}
interface OptionRequirements {
[key: Option]: OptionRequirement;
}
This seems very straightforward to me, however I get the following error:
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
What am I doing wrong?Solutionsource: stackoverflow \u2197
You can use TS "in" operator and do this: enum Options { ONE = 'one', TWO = 'two', THREE = 'three', } interface OptionRequirement { someBool: boolean; someString: string; } type OptionRequirements = { [key in Options]: OptionRequirement; // Note the "in" operator. } More about the in operator
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/e7a127611793333fa4c74c2ed78a0713c6e64c2b408f3bd467f8326c7fdb44d2hash \u00b7 e7a127611793333fa4c74c2ed78a0713c6e64c2b408f3bd467f8326c7fdb44d2