我正在尝试使用以下模式:
enum Option {
ONE = 'one',
TWO = 'two',
THREE = 'three'
}
interface OptionRequirement {
someBool: boolean;
someString: string;
}
interface OptionRequirements {
[key: Option]: OptionRequirement;
}
这对我来说似乎很简单,但是我收到以下错误:
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
我做错了什么?
最佳答案
您可以使用 TS“in”运算符并执行此操作:
enum Options {
ONE = 'one',
TWO = 'two',
THREE = 'three',
}
interface OptionRequirement {
someBool: boolean;
someString: string;
}
type OptionRequirements = {
[key in Options]: OptionRequirement; // Note that "key in".
}
关于javascript - 索引签名参数类型不能是联合类型。考虑改用映射对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54438012/