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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:47:02  来源:igfitidea点击:

An index signature parameter type cannot be a union type. Consider using a mapped object type instead

javascripttypescript

提问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 Optionsis an enum, not a string, number, or symbol.

您定义中的问题是说您的接口的键应该是 type Options,其中Options是枚举,而不是字符串、数字或符号。

The key in Optionsmeans "for those specific keys that's in the union type Options".

key in Options“为这是在联合类型选择那些特定的键”的意思。

typealias is more flexible and powerful than interface.

typealias 比interface.

If your type does not need to be used in class, choose typeover interface.

如果您的类型不需要在课堂上使用,请选择typeover 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 anenumas 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