Typescript

实用类型(Utility Types)

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

  • Awaited<Type>获取异步操作返回值的类型
    这种类型用于对异步函数中的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<Promise<Promise>>;
type B = number

  • Partial<Type>
    返回一个给定类型的所有属性,该类型的所有属性都设置为可选
interface Person {
  name: string;
  age: string;
  hobit?: string;
}
// 定义了一个人类接口,但是想命名一个继承它所有属性的未知变量,所有属性为可选,这样ts就不会报错了
const newPerson: Partial<Person> = {
  name: '未定义的人类'
}
  • Required<Type>
    与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<Person>': age, hobit(2739)
  • Readonly<Type>
    设置一个类型的所有属性为只读模式,不能重新赋值,对于更深层次的无限制
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>
    构造属性键为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>
    通过从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<Type, Keys>
    和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>
    从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中提取可分配给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<Type>
    通过从类型中排除null和undefined来构造类型。
type T1 = NonNullable<string[] | null | undefined>;
// type T1 = string[]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值