FrankKai / FrankKai.github.io

FE blog
https://frankkai.github.io/
362 stars 39 forks source link

TypeScript类型体操 #262

Open FrankKai opened 2 years ago

FrankKai commented 2 years ago

TypeScript类型体操,核心思想是 通过类型生成新的类型! 记录一下type-challenges的题解,以及解题思路。

仓库地址:https://github.com/type-challenges/type-challenges/blob/main/README.zh-CN.md

2 - 获取函数返回类型

infer / (...args: any) => infer P https://github.com/type-challenges/type-challenges/issues/11520

3 - 实现 Omit

keyof / extends / in https://github.com/type-challenges/type-challenges/issues/11545

4 - 实现 Pick

keyof / extends / in https://github.com/type-challenges/type-challenges/issues/11021

7 - 实现 Readonly

readonly https://github.com/type-challenges/type-challenges/issues/11104

10 - 元组转合集

T[number] / extends / infer / never https://github.com/type-challenges/type-challenges/issues/11408

11 - 元组转换为对象

extends / readonly / in / T[number] https://github.com/type-challenges/type-challenges/issues/11105

12 - 可串联构造器

泛型组合 / Record / 伪return this https://github.com/type-challenges/type-challenges/issues/12073

14 - 第一个元素

extends / never / T[0] https://github.com/type-challenges/type-challenges/issues/11107

15 - 最后一个元素

...T / T['length'] / infer https://github.com/type-challenges/type-challenges/issues/11586

16 - 出堆

infer / ...T https://github.com/type-challenges/type-challenges/issues/11595

18 - 获取元组长度

extends / readonly / T['length'] https://github.com/type-challenges/type-challenges/issues/11109

43 - Exclude

extends / never https://github.com/type-challenges/type-challenges/issues/11111

189 - Awaited

infer / 递归 / Promise<T> https://github.com/type-challenges/type-challenges/issues/11747

268 - If

extends boolean / extends true https://github.com/type-challenges/type-challenges/issues/11404

533 - Concat

...T 解构 https://github.com/type-challenges/type-challenges/issues/11423

599 - Merge

keyof / in https://github.com/type-challenges/type-challenges/issues/11589

898 - Includes

infer / ...T 解构 / 递归 https://github.com/type-challenges/type-challenges/issues/11534

3057 - Push

...T 解构 https://github.com/type-challenges/type-challenges/issues/11527

3060 - Unshift

...T 解构 https://github.com/type-challenges/type-challenges/issues/11529

3312 - Parameters

infer https://github.com/type-challenges/type-challenges/issues/11539