简体   繁体   English

为什么 typescript 抱怨计算的属性名称?

[英]Why does typescript complain about a computed property name?

Using typescript Version 3.7.5.使用 typescript 版本 3.7.5。 I have these two simple types and I would expect that either both of them compile or neither do.我有这两种简单的类型,我希望它们要么编译要么都不编译。 However, typescript only complains about the second one.但是,typescript 只抱怨第二个。 Why is the second type invalid, but the first isn't?为什么第二种无效,但第一种无效?

// this works
type foo = {
  [key in 'xxx']: number
}

// this does not
type bar = {
  xxx: number
  [key in 'xxx']: number
}

Here are the compiler messages:以下是编译器消息:

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 right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.ts(2361)

See the code in action in the typescript playground .请参阅typescript 操场中的代码。

The first definition is a mapped type ( doc ), which binds key , so you can use it in the right-hand side, eg:第一个定义是一个映射类型doc ),它绑定了key ,所以你可以在右边使用它,例如:

type foo = {
  [key in 'xxx']: key
}

You cannot add properties to mapped types though.但是,您不能将属性添加到映射类型。 For example, for例如,对于

type foo = {
  [key in 'xxx']: key
   xxx: number
}

TypeScript will show an error about an expected } at the position of xxx . TypeScript 将在xxx的 position 处显示有关预期}的错误。

On the other hand,另一方面,

type bar = {
  xxx: number
  [key in 'xxx']: number
}

is a regular type literal, in which [key in 'xxx']: number is a computed property, for which the restrictions mentioned in the first two error messages hold.是一个常规类型文字,其中[key in 'xxx']: number是一个计算属性,前两个错误消息中提到的限制适用。

The third error message comes from TypeScript interpreting in as the regular expression-level in .第三条错误消息来自 TypeScript 将in解释in . You can see the same error here:您可以在此处看到相同的错误:

const error = key in 'xxx'

(and if you hover over key in your second example, you'll also see a Cannot find name 'key'. error) (如果您在第二个示例中使用 hover over key ,您还会看到Cannot find name 'key'.错误)

暂无
暂无

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

相关问题 为什么打字稿不会抱怨某些未定义的变量 - Why does typescript not complain about certain undefined variables 为什么TypeScript抱怨我的抽象类成员的实现? - Why does TypeScript complain about my implementation of an abstract class member? 为什么TypeScript在投射时不抱怨状态中不包含的属性? - Why does TypeScript not complain about properties not included in the state when casting? 为什么 TypeScript 抱怨枚举上的字符串索引? - Why does TypeScript complain about string index on an enum? 为什么TypeScript不抱怨带有计算键的Partial类型中的值不正确? - Why doesn't TypeScript complain about incorrect values in Partial types with computed keys? 为什么打字稿抱怨不可扩展性和依赖库的只读违规? - Why does typescript complain about non-extensibility and readonly violation of dependency library? 为什么它抱怨类型 {intrinsicattributes && true} 或类型 {intrinsicattributes && false} 不能使用 react 和 typescript 分配? - Why does it complain about type {intrinsicattributes && true} or type {intrinsicattributes && false} are not assignable using react and typescript? 类型上不存在属性:为什么 TypeScript 抱怨而 JavaScript 不抱怨? - Property does not exist on type: Why does TypeScript complain while JavaScript doesn't? 为什么TypeScript在计算数字时会报错? - Why does TypeScript complain when calculating numbers? 为什么 TypeScript 不抱怨返回类型不正确? - Why doesn't TypeScript complain about an incorrect return type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM