李恒道 发表于 2022-7-18 10:13:29

typescript T[number]的出处

目前看到的比较好的文章是
https://zenn.dev/luvmini511/articles/d89b3ad241e544
但是出于我的民族自豪感我的韩文狗屁不通
所以只能依靠翻译大概囫囵吞枣看一眼了
# 开始
我们在写代码的时候
存在一种类型风格
```
type Colors = ["white", "red", "black", "purple"]
type ColorsUnion = Colors // "white" | "red" | "black" | "purple"
```
我在想到底为什么
# 元组类型
https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types
解释了基本元组类型以及实例
元素是一种Array类型,他确定的知道包含哪种元素以及某个特定位置上具有哪种特定类型
```
type StringNumberPair = ;
```
粗略的说,他也是一种数组类型,他决定了元素的数量以及哪个元素在哪个位置
如果你向下翻阅还可以看到一个重要部分
```
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
T通过使用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
其中T是某种类型
并且K是可以分配给keyof T
相当于必须是T类型的子集
或可设置成number
如果T是包含数字索引签名的类型
# 索引签名
当我查找新线索,数字索引签名的时候
在https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types
找到了解释
我们可以描述可以进行索引的类型
例如a或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 // "white" | "red" | "black" | "purple"
我么使用Colors可以得到每一个返回值
同样
我们使用length也可以取出元素的数量
type ColorsLength = Colors["length"] // 4
# T
那么我们为什么不能使用Colors
首先想到的是
```
type Colors = ["white", "red", "black", "purple"]
type ColorsString = Colors //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,因为Array没有字符串索引签名。你可以使用那些关键字,但在Array没有。因为它没有字符串索引签名,T所以是不合法的。你可以使用T['length'],但是,因为Array确实有一个具有特定字符串的属性。使用string或number引用任何字符串或数字——这需要索引签名。
```
总结如下
1.array没有string索引签名
2.T因为他有一个特定的字符串可以使用
3.string或number表示所有字符串和数字,但这需要索引签名
我明白了,你需要一个索引签名才能访问所有的T
发表评论的人也是这么说的
interface Dictionary<Value> {
    : Value;
}
有了这个,我们可以使用T,当T是一个字典的时候,和T的值将是Value
而在数组的情况下,它有length一个特定的字符串,但它没有用,因为它没有T索引签名。
# 结尾
关于索引签名的解释我参考了之前的https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types,但是我担心它是正确的,因为它显示为“此页面已被弃用”并且它没有在最近的https://www.typescriptlang.org/docs/handbook/2/objects.html中列出。如果有任何错误,请告诉我!
# 补充
rubinTime也补充了一些资料
在二楼评论区~



rubinTime 发表于 2022-7-18 12:17:09

在 JavaScript 中我们可以通过 obj 的方式来动态访问一个对象属性(即计算属性),expression 表达式会先被执行,然后使用返回值来访问属性。而 TypeScript 中我们也可以通过类似的方式,只不过这里的 expression 要换成类型。接下来,我们来看个例子:
```ts
interface NumberRecord {
: number;
}

type PropType = NumberRecord; // number
```

这里,我们使用 string 这个类型来访问 NumberRecord。由于其内部声明了数字类型的索引签名,这里访问到的结果即是 number 类型。注意,其访问方式与返回值均是类型。







```ts
interface Foo {
propA: number;
propB: boolean;
propC: string;
}
type PropTypeUnion = Foo; // string | number | boolean
```


使用字面量联合类型进行索引类型访问时,其结果就是将联合类型每个分支对应的类型进行访问后的结果,重新组装成联合类型。索引类型查询、索引类型访问通常会和映射类型一起搭配使用,前两者负责访问键,而映射类型在其基础上访问键值类型,我们在下面一个部分就会讲到。

注意,在未声明索引签名类型的情况下,我们不能使用 NumberRecord 这种原始类型的访问方式,而只能通过键名的字面量类型来进行访问。

```ts
interface Foo {
propA: number;
}

// 类型“Foo”没有匹配的类型“string”的索引签名。
type PropAType = Foo;
```

王一之 发表于 2022-7-18 10:21:16

坏了,我第一眼以为是那个T开头的

李恒道 发表于 2022-7-18 10:22:05

王一之 发表于 2022-7-18 10:21
坏了,我第一眼以为是那个T开头的

哪个
Trump?
Tesla?
Xvideo?

王一之 发表于 2022-7-18 10:35:59

李恒道 发表于 2022-7-18 10:22
哪个
Trump?
Tesla?

T*mb*r?

李恒道 发表于 2022-7-18 10:51:30

王一之 发表于 2022-7-18 10:35
T*mb*r?

那不就是个博客网站吗!哥哥为啥这么忌讳

李恒道 发表于 2022-7-18 10:57:10

王一之 发表于 2022-7-18 10:35
T*mb*r?

话说哥哥也没事来做体操?

王一之 发表于 2022-7-18 11:06:24

李恒道 发表于 2022-7-18 10:57
话说哥哥也没事来做体操?

老了,动不了

李恒道 发表于 2022-7-18 14:27:31

rubinTime 发表于 2022-7-18 12:17
在 JavaScript 中我们可以通过 obj 的方式来动态访问一个对象属性(即计算属性),express ...

我擦
哥哥在哪看到的
我昨天找了好久没找到相关文章

rubinTime 发表于 2022-7-18 14:57:49

李恒道 发表于 2022-7-18 14:27
我擦
哥哥在哪看到的
我昨天找了好久没找到相关文章

哥哥我是看掘金小册里面有章看到过这个
页: [1] 2 3
查看完整版本: typescript T[number]的出处