TypeScript 类型文字中的计算属性名称必须直接引用内置符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44110641/
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 04:34:23  来源:igfitidea点击:

TypeScript A computed property name in a type literal must directly refer to a built-in symbol

typescript

提问by zurfyx

ERROR in ...component.ts (..,..): A computed property name in a type literal must directly refer to a built- in symbol. ...: Cannot find name 'any'.

...component.ts (..,..) 中的错误:类型文字中的计算属性名称必须直接引用内置符号。...:找不到名称“任何”。

I'm looking forward an object which contains strings which have other string, i.e:

我期待一个包含具有其他字符串的字符串的对象,即:

{ abc: 'xyz' }

What I did:

我做了什么:

foo: { [string]: string };

回答by zurfyx

An identifying name on the computed value is required:

计算值上的识别名称是必需的:

foo: { [bar: string]: string };

回答by Nickofthyme

I had a similar issue after building. The two issues I faced were with using numbers and or enum values as keys in objects. Just to help those that see this in the future.

构建后我遇到了类似的问题。我面临的两个问题是使用数字和/或枚举值作为对象中的键。只是为了帮助那些将来看到这一点的人。

Enums as keys

枚举作为键

export enum MyEnum {
  one = 'stringOne',
  two = 'stringTwo',
}

export const someMap = {
  [ MyEnum.one ]: 'valueOne',
  [ MyEnum.two ]: 'valueTwo',
};

This will transpile someMapto a type that look something like...

这将转换someMap为一种看起来像......

export declare const someMap: {
  [ MyEnum.one ]: string;
  [ MyEnum.two ]: string;
};

Note the keys are still the enum value and not strings, typescript/angular does not like that because it is expecting something like...

请注意,键仍然是枚举值而不是字符串,打字稿/角度不喜欢那样,因为它期待类似......

export declare const someMap: {
  [ x: string ]: string;
};

So two possible fixes are...

所以两个可能的修复是......

1) Assign explicit type to someMap

1) 将显式类型分配给 someMap

export interface ForceStringType {
  [product: string]: string;
}
export const someMap: ForceStringType = {
  [ MyEnum.one ]: 'valueOne',
  [ MyEnum.two ]: 'valueTwo',
};

2) Assign stringtype to keys of someMap

2)分配string类型的键someMap

export const someMap: ForceStringType = {
  [ MyEnum.one as string ]: 'valueOne',
  [ MyEnum.two as string ]: 'valueTwo',
};

Numbers as keys

数字作为键

const CONSTANT_ONE = 123;
const CONSTANT_TWO = 321;

export const someMap = {
  [ CONSTANT_ONE ]: 'valueOne',
  [ CONSTANT_TWO ]: 'valueTwo',
};

This will transpile someMapto a type that look something like...

这将转换someMap为一种看起来像......

export declare const someMap: {
  [ CONSTANT_ONE ]: string;
  [ CONSTANT_TWO ]: string;
};

Note the keys are still the constant/number value and not strings, typescript/angular is again expecting something like...

请注意,键仍然是常量/数字值而不是字符串,打字稿/角度再次期待类似......

export declare const someMap: {
  [ x: string ]: string;
};

So one possible fix is...

所以一种可能的解决方法是......

Interpolate number as string for each key of someMap

为每个键插入数字作为字符串 someMap

export declare const someMap: {
  [ `${CONSTANT_ONE}` ]: string;
  [ `${CONSTANT_TWO}` ]: string;
};

Note: Accessing a value from someMapwith the constant/number as a key should not matter as typescript will coerce it to a string anyway, but probably best for overall consistency.

const valueOne: string = someMap[ CONSTANT_ONE ];
// vs
const valueOne: string = someMap[ `${CONSTANT_ONE}` ];

注意:someMap使用常量/数字作为键访问值应该无关紧要,因为打字稿无论如何都会将其强制为字符串,但可能最适合整体一致性。

const valueOne: string = someMap[ CONSTANT_ONE ];
// vs
const valueOne: string = someMap[ `${CONSTANT_ONE}` ];