这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助前期准备

本篇文章的编写目的是为了提升TS类型的书写质量,高质量的类型可以提高项目的可维护性并避免一些潜在的漏洞;

在学习本篇之前需要有一定的TS基础知识,在此基础上可以更好的完成各种类型的挑战,编写出属于自己的类型工具;

这里推荐我之前梳理的基础知识点 一份够用的TS常用特性总结 或 TS中文文档 ;

目前只完成了easy类型和部分medium类型的训练,后续会持续补充;

easyreadonly

实现Readonly,接收一个泛型参数,并返回一个完全一样的类型,只是所有属性都会被readonly所修饰。

type MyReadonly = {  readonly [P in keyof T] : T[P]}interface Todo {  title: string;  description: string;}const todoObj: MyReadonly = {  title: "Hey",  description: "foobar",};console.log(todoObj.title)todoObj.description = "barFoo"; // Error: cannot reassign a readonly property

first-of-array

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

type First = T extends [] ? never : T[0];type arr1 = ['a', 'b', 'c']type arr2 = [3, 2, 1]type head1 = First // expected to be 'a'type head2 = First // expected to be 3

tuple-to-object

实现TupleToObject,传入元组类型,将元组类型转换为对象类型,这个对象类型的键/值都是从元组中遍历出来。

type TupleToObject = {  [P in T[number]]: P;};const tuple = ["tesla", "model 3", "model X", "model Y"] as const;type result = TupleToObject; // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

length of tuple

创建一个通用的Length,接受一个readonly的数组,返回这个数组的长度。

type Length = T["length"];type tesla = ["tesla", "model 3", "model X", "model Y"];type spaceX = [  "FALCON 9",  "FALCON HEAVY",  "DRAGON",  "STARSHIP",  "HUMAN SPACEFLIGHT"];type teslaLength = Length; // expected 4type spaceXLength = Length; // expected 5

Exclude

从联合类型T中排除U的类型成员,来构造一个新的类型。

type MyExclude = T extends U ? never : T;type Result = MyExclude; // 'b' | 'c'

Awaited

假如我们有一个 Promise 对象,这个 Promise 对象会返回一个类型。在 TS 中,我们用 Promise 中的 T 来描述这个 Promise 返回的类型。请你实现一个类型,可以获取这个类型。 例如:Promise,请你返回 ExampleType 类型。

type MyAwaited = T extends PromiseLike ? MyAwaited : Ttype ExampleType = Promisetype Results = MyAwaited // string

IF

实现一个 IF 类型,它接收一个条件类型 C ,一个判断为真时的返回类型 T ,以及一个判断为假时的返回类型 F。 C 只能是 true 或者 false, T 和 F 可以是任意类型。

type If = C extends true ? T : F;type A = If; // expected to be 'a'type B = If; // expected to be 'b'

Concat

在类型系统里实现 JavaScript 内置的 Array.concat 方法,这个类型接受两个参数,返回的新数组类型应该按照输入参数从左到右的顺序合并为一个新的数组。

type Concat = [...T, ...U];type ResultConcat = Concat; // expected to be [1, 2]

Include

实现 Array.includes 方法,这个类型接受两个参数,返回的类型要么是 true 要么是 false。

type Includes =  U extends T[number] ? true : falsetype isPillarMen = Includes // expected to be `false`

Push

实现通用的Array.push类型。

type Push = [...T, U];type Resulted = Push; // [1, 2, '3']

Unshift

实现类型 Array.unshift类型。

type Unshift = [U, ...T];type UnshiftList = Unshift; // [0, 1, 2,]

Parameters

实现内置的 Parameters 类型。

type MyParameters any> = T extends (  ...args: infer U) => any  ? U  : never;const foo = (arg1: string, arg2: number): void => {};type FunctionParamsType = MyParameters; // [arg1: string, arg2: number]

ediumReturnType

不使用 ReturnType 实现 TypeScript 的 ReturnType 泛型。

type MyReturnType = T extends (...args: any[]) => infer R ? R : never;const fn = (v: boolean) => {  if (v) return 1;  else return 2;};type a = MyReturnType; // 应推导出 "1 | 2"

Omit

不使用 Omit 实现 TypeScript 的 Omit 泛型。Omit 会创建一个省略 K 中字段的 T 对象。

type MyOmit = {  [key in Exclude]: T[key];};interface Todo {  title: string;  description: string;  completed: boolean;}type TodoPreview = MyOmit;const todo: TodoPreview = {  completed: false,};

ReadOnly2

实现一个通用MyReadonly2,它带有两种类型的参数T和K。 K指定应设置为Readonly的T的属性集。如果未提供K,则应使所有属性都变为只读,就像普通的Readonly一样。

type MyReadonly2 = {  readonly [P in K]: T[P];} & {  [P in Exclude]: T[P];};interface Todo {  title: string;  description: string;  completed: boolean;}const todos: MyReadonly2 = {  title: "Hey",  description: "foobar",  completed: false,};todos.title = "Hello"; // Error: cannot reassign a readonly propertytodos.description = "barFoo"; // Error: cannot reassign a readonly propertytodos.completed = true; // OK

DeepReadonly

实现一个通用的DeepReadonly,它将对象的每个参数及其子对象递归地设为只读。

type DeepReadonly = T extends Function  ? T  : {      readonly [K in keyof T]: K extends Object ? DeepReadonly : T[K];    };type X = {  x: {    a: 1;    b: "hi";  };  y: "hey";};type Expected = {  readonly x: {    readonly a: 1;    readonly b: "hi";  };  readonly y: "hey";};type Todo = DeepReadonly; // should be same as `Expected`

TupleToUnion

实现泛型TupleToUnion,它返回元组所有值的合集。

type TupleToUnion = T[number]type Arr = ['1', '2', '3']type Test = TupleToUnion // expected to be '1' | '2' | '3'

LastOfArray

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

type Last = T extends [...unknown[], infer R] ? R : nevertype arr1 = ['a', 'b', 'c']type arr2 = [3, 2, 1]type tail1 = Last // expected to be 'c'type tail2 = Last // expected to be 1

https://juejin.cn/post/7193917621069152311如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。