TypeScript 工具类型 - Utility Types
官方鏈接
Partial
構造一個 Type 的所有屬性都設置為 optional 的類型。 此實用程序將返回表示給定類型的所有子集的類型。
例子:
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", });測試結果:第一個輸入參數的 description 字段被第二個參數的同名字段覆蓋了。
如果把兩個操作數前的 … 都去掉,則失去了兩個對象做交集的效果,變成了求并集。
Required 的效果和 Optional 正好相反:
interface Props {a?: number;b?: string; }const obj: Props = { a: 5 };const obj2: Required<Props> = { a: 5 }; Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.Readonly
interface Todo {title: string; }const todo: Readonly<Todo> = {title: "Delete inactive users", };todo.title = "Hello"; Cannot assign to 'title' because it is a read-only property.Record<Keys,Type>
構造一個對象類型,其屬性鍵為 Keys,屬性值為 Type。 此實用程序可用于將一種類型的屬性映射到另一種類型。
例子:
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" }, };cats.boris;const cats: Record<CatName, CatInfo>Pick<Type, Keys>
通過從 Type 中選取一組屬性鍵(字符串文字或字符串文字的并集)來構造一個類型。
例子:
interface Todo {title: string;description: string;completed: boolean; }type TodoPreview = Pick<Todo, "title" | "completed">;const todo: TodoPreview = {title: "Clean room",completed: false, };todo;新的類型,是從 Todo 類型中把 title 和 completed 屬性摘出來。
Omit<Type, Keys>
通過從 Type 中選取所有屬性然后刪除鍵(字符串文字或字符串文字的并集)來構造一個類型。
同 Pick 相反。
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: TodoPreviewtype TodoInfo = Omit<Todo, "completed" | "createdAt">;const todoInfo: TodoInfo = {title: "Pick up kids",description: "Kindergarten closes at 5pm", };todoInfo;const todoInfo: TodoInfoExclude<Type, ExcludedUnion>
通過從 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<string | number | (() => void), Function>;type T2 = string | numberExtract<Type, Union>
通過從 Type 中提取可分配給 Union 的所有聯合成員來構造一個類型。取交集。
type T0 = Extract<"a" | "b" | "c", "a" | "f">;type T0 = "a" type T1 = Extract<string | number | (() => void), Function>;type T1 = () => voidNonNullable
通過從中排除 null 和 undefined 來構造一個類型。
type T0 = NonNullable<string | number | undefined>;type T0 = string | number type T1 = NonNullable<string[] | null | undefined>;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<<T>(arg: T) => T>;type T2 = [arg: unknown] type T3 = Parameters<typeof f1>;type T3 = [arg: {a: number;b: string; }] type T4 = Parameters<any>;type T4 = unknown[] type T5 = Parameters<never>;type T5 = never type T6 = Parameters<string>; Type 'string' does not satisfy the constraint '(...args: any) => any'.type T6 = never type T7 = Parameters<Function>; Type 'Function' does not satisfy the constraint '(...args: any) => any'.Type 'Function' provides no match for the signature '(...args: any): any'.type T7 = neverConstructorParameters
從構造函數類型的類型構造元組或數組類型。 它生成一個包含所有參數類型的元組類型(或者如果 Type 不是函數,則類型 never )。
type T0 = ConstructorParameters<ErrorConstructor>;type T0 = [message?: string] type T1 = ConstructorParameters<FunctionConstructor>;type T1 = string[] type T2 = ConstructorParameters<RegExpConstructor>;type T2 = [pattern: string | RegExp, flags?: string] type T3 = ConstructorParameters<any>;type T3 = unknown[]type T4 = ConstructorParameters<Function>; Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.Type 'Function' provides no match for the signature 'new (...args: any): any'.type T4 = neverReturnType
構造一個由函數 Type 的返回類型組成的類型。
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>() => T>;type T2 = unknown type T3 = ReturnType<<T extends U, U extends number[]>() => T>;type T3 = number[] type T4 = ReturnType<typeof f1>;type T4 = {a: number;b: string; } type T5 = ReturnType<any>;type T5 = any type T6 = ReturnType<never>;type T6 = never type T7 = ReturnType<string>; Type 'string' does not satisfy the constraint '(...args: any) => any'.type T7 = any type T8 = ReturnType<Function>; Type 'Function' does not satisfy the constraint '(...args: any) => any'.Type 'Function' provides no match for the signature '(...args: any): any'.type T8 = anyInstanceType
構造一個由 Type 中構造函數的實例類型組成的類型。
class C {x = 0;y = 0; }type T0 = InstanceType<typeof C>;type T0 = C type T1 = InstanceType<any>;type T1 = any type T2 = InstanceType<never>;type T2 = never type T3 = InstanceType<string>; Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.type T3 = any type T4 = InstanceType<Function>; Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.Type 'Function' provides no match for the signature 'new (...args: any): any'.type T4 = any更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的TypeScript 工具类型 - Utility Types的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 史上最火五一正式收官:旅游业大爆发 火车
- 下一篇: 智能手机销售持续疲软,高通 Q2 净利润