美文网首页
TypeScript 工具类型 - Utility Types

TypeScript 工具类型 - Utility Types

作者: 华山令狐冲 | 来源:发表于2021-08-16 10:44 被阅读0次

官方链接

Partial<Type>

构造一个 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<Type>

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: TodoPreview

type TodoInfo = Omit<Todo, "completed" | "createdAt">;

const todoInfo: TodoInfo = {
  title: "Pick up kids",
  description: "Kindergarten closes at 5pm",
};

todoInfo;
   
const todoInfo: TodoInfo

Exclude<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 | number

Extract<Type, Union>

通过从 Type 中提取可分配给 Union 的所有联合成员来构造一个类型。取交集。

type T0 = Extract<"a" | "b" | "c", "a" | "f">;
     
type T0 = "a"
type T1 = Extract<string | number | (() => void), Function>;
     
type T1 = () => void

NonNullable<Type>

通过从中排除 null 和 undefined 来构造一个类型。

type T0 = NonNullable<string | number | undefined>;
     
type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;
     
type T1 = string[]

Parameters<Type>

根据函数类型 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 = never

ConstructorParameters<Type>

从构造函数类型的类型构造元组或数组类型。 它生成一个包含所有参数类型的元组类型(或者如果 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 = never

ReturnType<Type>

构造一个由函数 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 = any

InstanceType<Type>

构造一个由 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

      本文链接:https://www.haomeiwen.com/subject/nqvjultx.html