typescript 索引签名参数类型不能是联合类型。考虑使用映射对象类型代替
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54438012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An index signature parameter type cannot be a union type. Consider using a mapped object type instead
提问by john maccarthy
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?
我究竟做错了什么?
回答by Nacho Justicia Ramos
You can use TS "in" operator and do this:
您可以使用 TS "in" 运算符并执行以下操作:
enum Options {
ONE = 'one',
TWO = 'two',
THREE = 'three',
}
interface OptionRequirement {
someBool: boolean;
someString: string;
}
interface OptionRequirements {
[key in Options]: OptionRequirement; // Note that "key in".
}
回答by unional
The simplest solution is to use Record
最简单的解决方案是使用 Record
type OptionRequirements = Record<Options, OptionRequirement>
You can also implement it yourself as:
你也可以自己实现它:
type OptionRequirements = {
[key in Options]: OptionRequirement;
}
This construct is only available to type
, but not interface
.
此构造仅适用于type
,但不适用于interface
。
The problem in your definition is saying the key of your interface should be of type Options
, where Options
is an enum, not a string, number, or symbol.
您定义中的问题是说您的接口的键应该是 type Options
,其中Options
是枚举,而不是字符串、数字或符号。
The key in Options
means "for those specific keys that's in the union type Options".
在key in Options
“为这是在联合类型选择那些特定的键”的意思。
type
alias is more flexible and powerful than interface
.
type
alias 比interface
.
If your type does not need to be used in class, choose type
over interface
.
如果您的类型不需要在课堂上使用,请选择type
over interface
。
回答by Kurkov Igor
I had some similar problem but my case was with another field property in interface so my solution as an example with optionalfield property with an enum for keys:
我有一些类似的问题,但我的情况是接口中的另一个字段属性,所以我的解决方案作为示例,带有可选字段属性和键枚举:
export enum ACTION_INSTANCE_KEY {
cat = 'cat',
dog = 'dog',
cow = 'cow',
book = 'book'
}
type ActionInstances = {
[key in ACTION_INSTANCE_KEY]?: number; // cat id/dog id/cow id/ etc // <== optional
};
export interface EventAnalyticsAction extends ActionInstances { // <== need to be extended
marker: EVENT_ANALYTIC_ACTION_TYPE; // <== if you wanna add another field to interface
}
回答by Hugo Laplace
You can't use anenum
as a key.
您不能将 anenum
用作键。
See this answer if you really want a workaround: https://github.com/Microsoft/TypeScript/issues/2491#issuecomment-260864535
如果您真的想要解决方法,请参阅此答案:https: //github.com/Microsoft/TypeScript/issues/2491#issuecomment-260864535