简体   繁体   English

在同一个接口中不能有常规属性和计算属性键

[英]Can't have a regular property and computed property keys in the same interface

TypeScript version: 3.6.4 TypeScript 版本:3.6.4

In my state, I want to have both a computed property and a regular property key inside an interface but TypeScript is having trouble parsing that.在我的 state 中,我想在接口内同时拥有计算属性和常规属性键,但 TypeScript 无法解析它。 Before creating a GitHub issue on the repository, I just want to make sure I'm not doing something wrong and wasting the devs' time.在存储库上创建 GitHub 问题之前,我只是想确保我没有做错什么并浪费开发人员的时间。

type Stat = 'hp' | 'attack' | 'defense' | 'speed';

interface State {
    [x in Stat]: number;
    type: string
}

I thought that would work but then TypeScript highlights type and says '}' expected. ts(1005)我认为这会起作用,但随后 TypeScript 突出显示type并说'}' expected. ts(1005) '}' expected. ts(1005) . '}' expected. ts(1005) If I put type at the top, it highlights [x in Stat] and says A computed property name must be of type 'string', 'number', 'symbol', or 'any'. ts(2464)如果我将type放在顶部,它会突出显示[x in Stat]并说A computed property name must be of type 'string', 'number', 'symbol', or 'any'. ts(2464) A computed property name must be of type 'string', 'number', 'symbol', or 'any'. ts(2464) & 'Stat' only refers to a type, but is being used as a value here.ts(2693) . A computed property name must be of type 'string', 'number', 'symbol', or 'any'. ts(2464) & 'Stat' only refers to a type, but is being used as a value here.ts(2693) If i comment out one of the 2 lines, TypeScript is totally okay with it.如果我注释掉这两行之一,TypeScript 完全可以接受。

Am I doing something wrong here or is this just not possible?我在这里做错了什么还是这不可能?

Without a better understanding of what you are working on, my proposal may be off target.如果没有更好地了解您的工作,我的建议可能会偏离目标。

You can probably work out something from mapped types and intersection:您可能可以从映射类型和交集中计算出一些东西:

/** Refefernce interface */
interface IStat {
  attack: unknown;
  defense: unknown;
  hp: unknown;
  speed: unknown;
}

/** (optional) `keyof` reference interface */
type Stat = keyof IStat

/** Mapped IStat properties intersected with `type` */
type State = {
  [K in keyof IStat]?: number; // change `number` with IStat[K] if you want the types from IStat;
} & { type: string; };

Here is a Typescript Playground for you to experiment further this approach.这是一个Typescript Playground供您进一步试验这种方法。

is this what you want?这是你想要的吗?

type Stat = 'hp' | 'attack' | 'defense' | 'speed';

type State = Record <Stat, number> & {
  type: string;
}

const s: State = {
  hp: 100,
  attack: 12,
  defense: 12,
  speed: 3,
  type: "myType"
}

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

相关问题 同一接口中的条件属性 - Conditional property in the same interface 我可以定义一个带有计算键的 Typescript 接口吗? - Can I define a Typescript interface with computed keys? Typescript接口属性不能使用“-”? - does typescript interface property can't use “-”? 如何在接口中使用字符串枚举类型作为计算属性名称? - How can I use a string enum type as a computed property name in an interface? 如何在TypeScript中定义没有属性的接口? - How to define a interface which doesn't have some property in TypeScript? 为什么不能将“具有属性的接口和具有索引签名的接口的交集”类型分配给仅具有该属性的 object? - Why can't type `intersection of an interface with a property and an interface with index signature` be assigned to an object with just the property? “接口中的计算属性名称必须直接引用内置符号” - “A computed property name in an interface must directly refer to a built-in symbol” 在 TypeScript 接口中使用字符串枚举值作为计算属性键 - Use string enum value in TypeScript interface as a computed property key 在同一接口中访问接口属性值 - Access interface property value in same interface Typescript 接口结合 object 属性上的 map 的键 - Typescript Interface combined with keys of map on object property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM