TS 工具类:
Partial<Type>
构建一个类型,将该类型的所有属性设置为可选。这个工具将返回一个表示给定类型的所有子集的类型。
Example
interface Todo {
title: string;
description: string;
}
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
return { ...todo, ...fieldsToUpdate };
}
const todo1 = {
title: "organize desk",
description: "clear clutter",
};
const todo2 = updateTodo(todo1, {
description: "throw out trash",
});
Required<Type>
构建一个由Type的所有属性组成的类型,且所有属性设置为必填(与Partial相反)。
Example
interface Props {
a?: number;
b?: string;
}
const obj: Props = { a: 5 };
const obj2: Required<Props> = { a: 5 }; // @errors: 2741
// 第八行 Obj2 会报错: Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.
Readonly<Type>
构造一个类型Type,Type 所有的属性都被设置为只读,意思是所有属性都不可以被更改。 Example
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: "Delete inactive users",
};
todo.title = "Hello"; // @errors: 2540
// 第 10 行报错: Cannot assign to 'title' because it is a read-only property.
Record<Keys, Type>
构造一个对象类型,该类型里面键的类型为 Keys,值的属性为 Type。这个工具可以用来将一个类型的属性映射到另一个类型。
Example
interface CatInfo {
age: number;
breed: string;
}
type CatName = "miffy" | "boris" | "mordred";
const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: "Persian" },
boris: { age: 5, breed: "Maine Coon" },
mordred: { age: 16, breed: "British Shorthair" },
};
Pick<Type, keys>
通过从 Type中 选取属性集合 Keys 来构造一个类型。
Example
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick<Todo, "title" | "completed">;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
};
Omit<Type, Keys>
构造一个类型,该类型的 key是去除 Type 中所有的 Keys 后组成的。
Example
interface Todo {
title: string;
description: string;
completed: boolean;
createdAt: number;
}
type TodoPreview = Omit<Todo, "description">;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
createdAt: 1615544252770,
};
todo;
const todo: TodoPreview
type TodoInfo = Omit<Todo, "completed" | "createdAt">;
const todoInfo: TodoInfo = {
title: "Pick up kids",
description: "Kindergarten closes at 5pm",
};
Exclude<UnionType, ExcludedMembers>
从 UnionType 的 key 中排出所有 ExcludedMembers 的key,组成一个类型。
Example
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<string | number | (() => void), Function>;
// type T2 = string | number
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; x: number }
| { kind: "triangle"; x: number; y: number };
type T3 = Exclude<Shape, { kind: "circle" }>
/*
type T3 = {
kind: "square";
x: number;
} | {
kind: "triangle";
x: number;
y: number;
}
*/
Extract<Type, Union>
提取 Type 和 Union 中相同的 key,组成一个类型。
Example
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
// type T0 = "a"
type T1 = Extract<string | number | (() => void), Function>;
//type T1 = () => void
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; x: number }
| { kind: "triangle"; x: number; y: number };
type T2 = Extract<Shape, { kind: "circle" }>
/*
type T2 = {
kind: "circle";
radius: number;
}
*/