TS08 TypeScript中的泛型工具

文章目录

  • 使用`is`进行类型收窄
  • 使用`&`进行属性的合并
  • `Partial`
  • `Required`
  • `Readonly`
  • `Record

使用is进行类型收窄

function isUser(person: Person): person is User {
  return person.type === 'user'
}

使用&进行属性的合并

使用&操作符可以实现属性合并

type User = {
  name: string
}

type Admin = {
  age: number
}

type Person = User | Admin; 
// 此时 Person 可以包含 name 或者可以包含 age

type Staff = User & Admin;
// 此时 Staff 必须同时包含 name 和age

Partial

使用Partial将所有类型属性变成可选

interface Todo {
  title: string;
  description: string;
}

function updateTodo(todo: Todo, fieldsToUpdate: Partial) {
  return { ...todo, ...fieldsToUpdate };
}

const todo1 = {
  title: "organize desk",
  description: "clear clutter",
};

const todo2 = updateTodo(todo1, {
  description: "throw out trash",
});

Partial的实现:

type Partial = { [P in keyof T]?: T[P] };

Required

Partial相反,将可选属性变为必选

interface Props {
  a?: number;
  b?: string;
}

const obj: Props = { a: 5 };

const obj2: Required = { a: 5 };
// 报错
// Property 'b' is missing in type '{ a: number; }' but required in type 'Required'.

Readonly

使用Readonly将类型设置为只读

interface Todo {
  title: string;
}

const todo: Readonly = {
  title: "Delete inactive users",
};

todo.title = "Hello";
// 报错
// Cannot assign to 'title' because it is a read-only property.

Record

使用Record生成的类型,包含keys对应的属性键名,键值为Type,常用来实现一种类型对另外一种类型的映射关系:

interface PageInfo {
  title: string;
}

type Page = 'home' | 'about' | 'concat'';

const nav: Record = {
  about: {title: 'about'},
  contact: {title: 'contact'},
  home: {title: 'home'},
}

Pick

使用Pick来在Type中拾取keys对应的属性

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Pick;

const todo: TodoPreview = {
  title: 'Clean Room',
  completed: false
}

Omit

Type中生成所有属性,并且移除key对应的属性

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPrview = Omit;

const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
};

Exclude

Type中移除ExcludedUnion中的所有类型(差集)

type T0 = Exclude<"a" | "b" | "c", "a">;
// type T0 = "b" | "c"

type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
// type T1 = "c"

type T2 = Exclude void), Function>;
// type T2 = string | number

Extract

生成TypeUnion的交集

type T0 = Extract<"a" | "b" | "c", "a" | "f">;
// type T0 = "a"

type T1 = Extract void), Function>;
// type T1 = () => void

NonNullable

生成不为nullundefined的类型

type T0 = NonNullable;
// type T0 = string | number

type T1 = NonNullable;
// type T1 = string[]

Parameters

生成对应的函数类型的Type的参数元组类型

declare function f1(arg: { a: number; b: string }): void;

type T0 = Parameters<() => string>;
// type T0 = []

type T1 = Parameters<(s: string) => void>;
// type T1 = [s: string]

type T2 = Parameters<(arg: T) => T>;
// type T2 = [arg: unknown]

type T3 = Parameters;
// type T3 = [arg: {
//   a: number;
//   b: string;
// }]

ReturnType

生成函数返回值对应的类型

declare function f1(): { a: number; b: string };

type T0 = ReturnType<() => string>;
// type T0 = string

type T1 = ReturnType<(s: string) => void>;
// type T1 = void

type T2 = ReturnType<() => T>;
// type T2 = unknown

type T4 = ReturnType;
// type T4 = {
//   a: number;
//   b: string;
// }

ThisParameterType

对于一个函数的this的类型,可以通过在参数中对进行声明:

function toHex(this: Number) {
  return this.toString(16);
}

这时可以使用ThisParameterType返回this类型,如果函数没有声明this类型,那么会返回unknown

function numberToString(n: ThisParameterType) {
  return toHex.apply(n);
}

参考

  • TS

你可能感兴趣的:(TypeScript)