简体   繁体   English

类型文字中的计算属性名称必须引用类型为文字类型或“唯一符号”类型的表达式

[英]A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type

Ultimately I would like to compute something as below最终我想计算如下

const configOne = {
  default: {master: 0},
  sg: {master: 1},
}

const configTwo = {
  default: {master: 1}
}

Basically the object must have the default as mandatory properties, then the remaining properties can be optional, but they must be of country prefix.基本上object必须有default的强制属性,然后其余属性可以是可选的,但它们必须是国家前缀。 Below is the attempt I have以下是我的尝试

enum CID {
  'tw',
  'sg',
  'vn',
}

interface IIndividualConfig {
  master: 0 | 1;
}

type IConfig = {
  default: IIndividualConfig;
  [key in CID]?: IIndividualConfig;
}


const configOne: IConfig = {
  default: { master: 0 },
  sg: {master: 1}
}


const configTwo: IConfig = {
  default: { master: 1 }
}

And below is the error I've got at [key in CID]下面是我在[key in CID]处遇到的错误

A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1170)
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)

The problem occurs because you've defined default in the definition while using index signature such as below出现问题是因为您在使用如下index signature时在定义中定义了default

type IConfig = {
  default: IIndividualConfig; // <= Causing error
  [key in CID]?: IIndividualConfig;
}

Possible Workaround 1:可能的解决方法 1:

type IConfig = {
  [key in CID | 'default']?: IIndividualConfig;
}

This will not gives you error, but probably doesn't 100% fit the use case, since default is no longer mandatory.这不会给你错误,但可能不会 100% 适合用例,因为default不再是强制性的。 Hence may want to look at option 2因此可能想看看选项 2

Workaround 2:解决方法 2:

type IDefaultConfig = { default: IIndividualConfig; }
type IConfig = IDefaultConfig & {[key in CID]?: IIndividualConfig; };

Now by using intersection , we combined two types into a single type, default will be mandatory, and other key are optional and they must be of type CID if defined现在通过使用intersection ,我们将两种类型组合成一个类型, default是强制的,其他键是可选的,如果定义它们必须是CID类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript - 计算属性名称的类型必须为“字符串”、“数字”、“符号”或“任意” - TypeScript - A computed property name must be of type 'string', 'number', 'symbol', or 'any' 打字稿错误:计算的属性名称必须是“字符串”、“数字”、“符号”或“任何”类型 - Typescript error: A computed property name must be of type 'string', 'number', 'symbol', or 'any' 模板文字类型等效 - Template Literal type equivalent 流在交集类型中丢失文字类型 - Flow lose literal type in intersection type typescript - 用于输入字符串的字符串文字类型 - typescript - string literal type to type string 基于对象属性文字类型的脱节联合类型 - Disjoint Union types based on object property literal type Typescript - 索引表达式参数必须是&#39;string&#39;,&#39;number&#39;,&#39;symbol&#39;或&#39;any&#39;类型 - Typescript - An index expression argument must be of type 'string', 'number', 'symbol' or 'any' 使用字符串定义类型与 object 文字属性 - Define Type with strings versus object literal properties 将字符串作为变量传递给文字方法类型提示 - Pass string as variable to literal method type hint TypeScript中定义的对象文字键的类型是什么? - What is the type of an object literal key as defined in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM