墨香阁
| 分享生活的点滴

TypeScript 工具类型

2025年01月08日 02:26:46
7 views
4 min read
前端
TypeScript 工具类型

TypeScript 工具类型

TypeScript 提供了一系列内置的工具类型(Utility Types),用于简化常见类型操作。这些工具类型基于 TypeScript 的类型系统,能够帮助开发者更高效地定义和操作类型。以下是常用的工具类型及其用途:


1. Partial<T>

  • 用途: 将类型 T 的所有属性变为可选。

  • 示例:

    typescript
    interface User { name: string; age: number; } type PartialUser = Partial<User>; // 等同于 { name?: string; age?: number; }

2. Required<T>

  • 用途: 将类型 T 的所有属性变为必填。

  • 示例:

    typescript
    interface User { name?: string; age?: number; } type RequiredUser = Required<User>; // 等同于 { name: string; age: number; }

3. Readonly<T>

  • 用途: 将类型 T 的所有属性变为只读。

  • 示例:

    typescript
    interface User { name: string; age: number; } type ReadonlyUser = Readonly<User>; // 等同于 { readonly name: string; readonly age: number; }

4. Record<K, T>

  • 用途: 创建一个对象类型,其键为 K,值为 T

  • 示例:

    typescript
    type UserRoles = Record<string, boolean>; // 等同于 { [key: string]: boolean; } const roles: UserRoles = { admin: true, user: false, };

5. Pick<T, K>

  • 用途: 从类型 T 中选取指定的属性 K

  • 示例:

    typescript
    interface User { name: string; age: number; email: string; } type UserNameAndAge = Pick<User, 'name' | 'age'>; // 等同于 { name: string; age: number; }

6. Omit<T, K>

  • 用途: 从类型 T 中排除指定的属性 K

  • 示例:

    typescript
    interface User { name: string; age: number; email: string; } type UserWithoutEmail = Omit<User, 'email'>; // 等同于 { name: string; age: number; }

7. Exclude<T, U>

  • 用途: 从类型 T 中排除可以赋值给 U 的类型。

  • 示例:

    typescript

    复制

    type T = 'a' | 'b' | 'c';
    type U = 'a' | 'b';
    
    type Result = Exclude<T, U>;
    // 等同于 'c'
    

8. Extract<T, U>

  • 用途: 从类型 T 中提取可以赋值给 U 的类型。

  • 示例:

    typescript
    type T = 'a' | 'b' | 'c'; type U = 'a' | 'b'; type Result = Extract<T, U>; // 等同于 'a' | 'b'

9. NonNullable<T>

  • 用途: 从类型 T 中排除 nullundefined

  • 示例:

    typescript
    type T = string | number | null | undefined; type Result = NonNullable<T>; // 等同于 string | number

10. ReturnType<T>

  • 用途: 获取函数类型 T 的返回值类型。

  • 示例:

    typescript
    function getUser() { return { name: 'Alice', age: 25 }; } type User = ReturnType<typeof getUser>; // 等同于 { name: string; age: number; }

11. Parameters<T>

  • 用途: 获取函数类型 T 的参数类型组成的元组。

  • 示例:

    typescript
    function add(a: number, b: number): number { return a + b; } type AddParams = Parameters<typeof add>; // 等同于 [number, number]

12. ConstructorParameters<T>

  • 用途: 获取构造函数类型 T 的参数类型组成的元组。

  • 示例:

    typescript
    class Person { constructor(public name: string, public age: number) {} } type PersonParams = ConstructorParameters<typeof Person>; // 等同于 [string, number]

13. InstanceType<T>

  • 用途: 获取构造函数类型 T 的实例类型。

  • 示例:

    typescript
    class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } type PersonInstance = InstanceType<typeof Person>; // 等同于 Person

14. ThisParameterType<T>

  • 用途: 提取函数类型 Tthis 参数类型。

  • 示例:

    typescript
    function greet(this: { name: string }) { console.log(`Hello, ${this.name}`); } type ThisType = ThisParameterType<typeof greet>; // 等同于 { name: string }

15. OmitThisParameter<T>

  • 用途: 移除函数类型 Tthis 参数。

  • 示例:

    typescript
    function greet(this: { name: string }) { console.log(`Hello, ${this.name}`); } type GreetWithoutThis = OmitThisParameter<typeof greet>; // 等同于 () => void

16. Awaited<T>

  • 用途: 获取 Promise 的解析值类型(适用于嵌套 Promise)。

  • 示例:

    typescript
    type T = Awaited<Promise<Promise<string>>>; // 等同于 string

17. Uppercase<T>Lowercase<T>Capitalize<T>Uncapitalize<T>

  • 用途: 操作字符串字面量类型的大小写。

  • 示例:

    typescript
    type T = Uppercase<'hello'>; // 等同于 'HELLO' type T2 = Lowercase<'HELLO'>; // 等同于 'hello' type T3 = Capitalize<'hello'>; // 等同于 'Hello' type T4 = Uncapitalize<'Hello'>; // 等同于 'hello'

总结

TypeScript 的工具类型极大地简化了类型操作,例如:

  • PartialRequired 用于调整属性的可选性。
  • PickOmit 用于选择或排除属性。
  • ReturnTypeParameters 用于提取函数类型信息。
  • Record 用于创建键值对类型。
  • Awaited 用于处理 Promise 类型。

这些工具类型可以帮助开发者更高效地定义和操作复杂类型,提升代码的可读性和可维护性。

© 2025 . 保留所有权利.

原始文章发表于 2025年01月08日 02:26:46

发表留言

全部留言 (0)

暂无留言,成为第一个留言的人吧!