Typescript体操类型学习1

文章目录

  • [TS 类型体操练习](#TS 类型体操练习)
    • [Tuple to Union](#Tuple to Union)
    • [Tuple to Object](#Tuple to Object)
    • [First of Array](#First of Array)
    • [Last of Array](#Last of Array)
    • Pop
    • [Length of Tuple](#Length of Tuple)

TS 类型体操练习

Tuple to Union

Implement a generic TupleToUnion<T> which covers the values of a tuple to its values union.

TupleToUnion 的目标是将元组类型中的元素类型转换为联合类型。

复制代码
type Arr = ['1', '2', '3']

type Test = TupleToUnion<Arr> // expected to be '1' | '2' | '3'

实现:

jsx 复制代码
type TupleToUnion<T extends any[]> = T[number];

使用索引访问类型 T[number] ,它会返回元组 T 中所有元素的联合类型

因为元组的索引是数字类型,所以 T[number] 表示取出元组中所有位置的类型,然后将它们合并成一个联合类型。

Tuple to Object

Given an array, transform it into an object type and the key/value must be in the provided array

将一个元组类型转换为对象类型,这个对象类型的键/值和元组中的元素对应。

复制代码
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const

type result = TupleToObject<typeof tuple> // expected { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

实现:

jsx 复制代码
type TupleToObject<T extends readonly any[]> = {
	[K in T[number]]: K
}

上一题说到,T[number] 可以返回元组T中所有元素的联合类型,拿到起所有类型后,使用in 进行遍历,就可以达到 Tuple to Object 效果

First of Array

Implement a generic First<T> that takes an Array T and returns its first element's type.

实现一个First<T>泛型,它接受一个数组T并返回它的第一个元素的类型。

复制代码
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]

type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3

实现:

第一反应就是直接取索引为0的一个元素类型来实现, type First<T extends any\[\]> = T0;

但当遇到Expect<Equal<First<[]>, never>>:

所以要考虑当数组为空时,是取不到第一个元素索引的

因此最终实现:

jsx 复制代码
type First<T extends any[]> = T extends [] ? never : T[0];

Last of Array

Implement a generic Last<T> that takes an Array T and returns its last element.

实现一个Last<T>泛型,它接受一个数组T并返回其最后一个元素的类型。

复制代码
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]

type tail1 = Last<arr1> // expected to be 'c'
type tail2 = Last<arr2> // expected to be 1

实现:

First of Array 一样, 要进行空数组判断,如果是空数组,就返回 never 类型, 表示永远不会发生;使用剩余运算,infer推断最后一个元素之前的所有元素,保留最后一个元素推断 last ,如果满足非空数组,则返回最后一个元素类型。

jsx 复制代码
type Last<T extends any[]> = T extends [...infer _, infer last] ? last : never;

Pop

Implement a generic Pop<T> that takes an Array T and returns an Array without it's last element.

实现一个泛型Pop<T>,它接受一个数组T,并返回一个由数组T的前 N-1 项(N 为数组T的长度)以相同的顺序组成的数组。

复制代码
type arr1 = ['a', 'b', 'c', 'd']
type arr2 = [3, 2, 1]

type re1 = Pop<arr1> // expected to be ['a', 'b', 'c']
type re2 = Pop<arr2> // expected to be [3, 2]

这里的case要求:

所以我们不能将空数组,直接返回never,而是返回\[\]类型

实现:

jsx 复制代码
type Pop<T extends any[]> = T extends [...infer remain, infer last]
  ? remain
  : [];

Length of Tuple

For given a tuple, you need create a generic Length, pick the length of the tuple

创建一个Length泛型,这个泛型接受一个只读的元组,返回这个元组的长度。

复制代码
type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']

type teslaLength = Length<tesla> // expected 4
type spaceXLength = Length<spaceX> // expected 5

实现:

在 TypeScript 中,数组是具有 length 属性的对象,该属性表示其长度, 所以可以直接这样获取元组的长度:

jsx 复制代码
type Length<T extends any[]> = T["length"];

当我们调用 Length<typeof tesla> 时,typeof tesla 是一个只读数组。我们无法将只读数组分配给 any[] 类型,因此我们需要进一步限制 T 仅允许只读数组:


相关推荐
searchforAI8 分钟前
我的Obsidian知识库,现在可以自动剪藏笔记到本地了
人工智能·笔记·学习·音视频·ai工具·obsidian·视频总结
周末也要写八哥13 分钟前
Visual C++6.0下载安装流程及PDF学习手册资源
c++·学习·pdf
坤坤藤椒牛肉面32 分钟前
C++学习--类和对象
学习
QFIUNE1 小时前
使用 MMseqs2 计算多个 DTI 数据集的蛋白序列相似度
linux·python·ubuntu
楼田莉子1 小时前
C++20新特性:Range库
开发语言·c++·后端·学习·c++20
段一凡-华北理工大学1 小时前
工业领域的Hadoop架构学习~系列文章02:HDFS架构深度剖析
大数据·人工智能·hadoop·学习·架构·高炉炼铁
星恒随风1 小时前
C语言数据结构排序算法详解(上):从插入排序、希尔排序到选择排序、堆排序
c语言·数据结构·笔记·学习·排序算法
Vick_Zhang1 小时前
ubuntu上rabbitmq
服务器·ubuntu·rabbitmq
晓杰'2 小时前
从0到1实现Balatro游戏后端(5):得分计算与单局结算流程实现
后端·typescript·node.js·游戏开发·项目实战·nestjs·webscoket
月夜的风吹雨2 小时前
Linux 基础开发工具详解:从 yum 到 gdb 实战指南
linux·git·ubuntu·centos·vim