Typescript

实用类型(Utility Types)

TypeScript提供了几种实用类型来促进常见的类型转换。这些实用类型可在全局范围内使用。

  • Awaited获取异步操作返回值的类型
    这种类型用于对异步函数中的await或Promises上的.then()方法等操作进行建模,特别是它们递归打开Promises的方式。
async function test() {
  let aa:Promise<string> = new Promise((resolve) => resolve('namw'));
  // 上面的相当于Promise.resolve('namw');
  let myNames: Awaited<typeof aa> = await aa;
  console.log(myNames) // 'namw'
  // 实际上myNames为string类型,方便提取异步resolve返回值
}
test();

获取的是递归异步返回类型
type B = Awaited>;
type B = number

  • Partial
    返回一个给定类型的所有属性,该类型的所有属性都设置为可选
interface Person {
  name: string;
  age: string;
  hobit?: string;
}
// 定义了一个人类接口,但是想命名一个继承它所有属性的未知变量,所有属性为可选,这样ts就不会报错了
const newPerson: Partial<Person> = {
  name: '未定义的人类'
}
  • Required
    与Partial类型相反,所有属性设置为必填属性
interface Person {
  name: string;
  age: string;
  hobit?: string;
}
 
const newPerson: Required<Person> = {
  name: '未定义的人类'
}
// Type '{ name: string; }' is missing the following properties from type 'Required': age, hobit(2739)
  • Readonly
    设置一个类型的所有属性为只读模式,不能重新赋值,对于更深层次的无限制
interface Person {
  name: string;
  age: string;
  hobit?: string;
}
 
let newPerson: Readonly<Person> = {
  name: '未定义的人类',
  age: '14'
}
newPerson.age = '15'
// Cannot assign to 'age' because it is a read-only property.(2540)

// 下面无报错
interface Person {
  name: string;
  age: {
    virtual:number;
    truth:number;
  };
  hobit?: string;
}
 
const newPerson: Readonly<Person> = {
  name: '未定义的人类',
  age: {
    virtual: 12,
    truth: 13
  }
}
newPerson.age.truth = 14;
  • Record
    构造属性键为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上有CatName个属性,每个属性对应的值类型为CatInfo
cats.boris;
  • Pick
    通过从Type上获取类型Keys,组合为新的类型
interface Todo {
  title: string;
  description: string;
  completed: boolean;
}
 
 // 从Todo上获取属性"title" | "completed"
type TodoPreview = Pick<Todo, "title" | "completed">;
 
const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
};
 
todo;
  • Omit
    和Pick相反,移除Keys属性
interface Todo {
  title: string;
  description: string;
  completed: boolean;
}
 
 // 从Todo上移除属性"description"
type TodoPreview = Omit<Todo, "description">;
 
const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
};
 
todo;
  • Exclude
    从UnionType中排除ExcludedMembers,组合为新的类型,
type T0 = Exclude<"a" | "b" | "c", "a">;
// type T0 = "b" | "c"
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 T0 = Extract<"a" | "b" | "c", "a" | "f">;
// type T0 = "a"
type Shape = { kind: "circle"; radius: number }
  | { kind: "square"; x: number }
  | { kind: "triangle"; x: number; y: number };
type T3 = Extract<Shape, { kind: "circle" }> 
 /* type T3 = {
    kind: "circle";
    radius: number;
} */
  • NonNullable
    通过从类型中排除null和undefined来构造类型。
type T1 = NonNullable<string[] | null | undefined>;
// type T1 = string[]

你可能感兴趣的:(typescript,typescript,javascript)