typescript T[number]的出处

我们在写代码的时候
存在一种类型风格

type Colors = ["white", "red", "black", "purple"]
type ColorsUnion = Colors[number] // "white" | "red" | "black" | "purple"

我在想到底为什么

元组类型

TypeScript: Documentation - Object Types
解释了基本元组类型以及实例
元素是一种Array类型,他确定的知道包含哪种元素以及某个特定位置上具有哪种特定类型

type StringNumberPair = [string, number];

粗略的说,他也是一种数组类型,他决定了元素的数量以及哪个元素在哪个位置
如果你向下翻阅还可以看到一个重要部分

simple tuple types like these are equivalent to types which are versions of Arrays that declare properties for specific indexes, and that declare length with a numeric literal type.
元组类型与声明特定索引以及长度的数字字面量类型的数组相等
interface StringNumberPair {
  // specialized properties
  length: 2;
  0: string;
  1: number;

  // Other 'Array<string | number>' members...
  slice(start?: number, end?: number): Array<string | number>;
}

元组类型interface与此相同

T[K]

T[K]通过使用K在T上使用来访问获得的类型
https://github.com/Microsoft/TypeScript/pull/11929

interface Thing {
    name: string;
    width: number;
    height: number;
    inStock: boolean;
}

type P1 = Thing["name"];  // string
type P2 = Thing["width" | "height"];  // number
type P3 = Thing["name" | "inStock"];  // string | boolean

形式的索引访问类型T[K]
其中T是某种类型
并且K是可以分配给keyof T
相当于必须是T类型的子集
或可设置成number
如果T是包含数字索引签名的类型

索引签名

当我查找新线索,数字索引签名的时候
在https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types
找到了解释
我们可以描述可以进行索引的类型
例如a[10]或ageMap['daniel']
可索引类型有一个索引签名
他描述了我们可以用来索引对象的类型
以及索引相应返回的类型
换句话说
索引签名可以描述用于索引的类型以及返回的对应类型
例如

interface Thing {
    name: string;
    width: number;
    height: number;
    inStock: boolean;
}

type P1 = Thing["name"];  // string

索引的类型是name,返回了一个string
顺便说一句,索引只存在两种类型,即number以及string
如果使用string,追寻的是字符串索引类型
如果使用number,追寻的是数字索引类型

推理

那么我们来看这个例子

type Colors = ["white", "red", "black", "purple"]

首先,由于Colors是一个元组类型,interface因此认为与下相同

interface Colors {
  length: 4;
  0: "white";
  1: "red";
  2: "black";
  3: "purple";
}

Colors包含一个数字索引签名,所以他可以被索引成1/2/3、number
type ColorsUnion = Colors[number] // "white" | "red" | "black" | "purple"
我么使用Colors[number]可以得到每一个返回值
同样
我们使用length也可以取出元素的数量
type ColorsLength = Colors["length"] // 4

T[string]

那么我们为什么不能使用Colors[string]
首先想到的是

type Colors = ["white", "red", "black", "purple"]
type ColorsString = Colors[string] //Type 'Colors' has no matching index signature for type 'string'

没有string适用于索引签名
即使我在interface中将数字索引改成字符串也没有任何作用
那length不是string吗?
我想,我又检查了一边
stackoverflow有人和我提了一样的问题
https://stackoverflow.com/questions/59187941/whats-the-tnumber-mean-in-typescript-code

你不能使用T[string],因为Array没有字符串索引签名。你可以使用那些关键字,但在Array没有。因为它没有字符串索引签名,T[string]所以是不合法的。你可以使用T['length'],但是,因为Array确实有一个具有特定字符串的属性。使用string或number引用任何字符串或数字——这需要索引签名。

总结如下
1.array没有string索引签名
2.T[length]因为他有一个特定的字符串可以使用
3.string或number表示所有字符串和数字,但这需要索引签名
我明白了,你需要一个索引签名才能访问所有的T[string]
发表评论的人也是这么说的
interface Dictionary {

}
有了这个,我们可以使用T[string],当T是一个字典的时候,和T[string]的值将是Value
而在数组的情况下,它有length一个特定的字符串,但它没有用,因为它没有T[string]索引签名。

结尾

关于索引签名的解释我参考了之前的https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types,但是我担心它是正确的,因为它显示为“此页面已被弃用”并且它没有在最近的https://www.typescriptlang.org/docs/handbook/2/objects.html中列出。如果有任何错误,请告诉我!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值