1、type和interface有什么区别?
2、ts中内置工具类型有哪些?
TypeScript 提供了许多内置的工具类型(Utility Types),用于简化类型操作和增强类型系统的表达能力。
1. Partial<T>
-
作用:将类型
T的所有属性变为可选。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 interface User { name: string; age: number; } type PartialUser = Partial<User>; // 等价于: // type PartialUser = { // name?: string; // age?: number; // }
2. Required<T>
-
作用:将类型
T的所有属性变为必选。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 interface User { name?: string; age?: number; } type RequiredUser = Required<User>; // 等价于: // type RequiredUser = { // name: string; // age: number; // }
3. Readonly<T>
-
作用:将类型
T的所有属性变为只读。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 interface User { name: string; age: number; } type ReadonlyUser = Readonly<User>; // 等价于: // type ReadonlyUser = { // readonly name: string; // readonly age: number; // }
4. Record<K, T>
-
作用:创建一个对象类型,其键为
K,值为T。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 type UserRoles = Record<string, boolean>; // 等价于: // type UserRoles = { // [key: string]: boolean; // }
5. Pick<T, K>
-
作用:从类型
T中选取指定的属性K。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 interface User { name: string; age: number; email: string; } type UserNameAndAge = Pick<User, 'name' | 'age'>; // 等价于: // type UserNameAndAge = { // name: string; // age: number; // }
6. Omit<T, K>
-
作用:从类型
T中排除指定的属性K。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 interface User { name: string; age: number; email: string; } type UserWithoutEmail = Omit<User, 'email'>; // 等价于: // type UserWithoutEmail = { // name: string; // age: number; // }
7. Exclude<T, U>
-
作用:从类型
T中排除可以赋值给U的类型。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 type T = 'a' | 'b' | 'c'; type U = 'a' | 'b'; type Result = Exclude<T, U>; // 等价于: // type Result = 'c'
8. Extract<T, U>
-
作用:从类型
T中提取可以赋值给U的类型。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 type T = 'a' | 'b' | 'c'; type U = 'a' | 'b'; type Result = Extract<T, U>; // 等价于: // type Result = 'a' | 'b'
9. NonNullable<T>
-
作用:从类型
T中排除null和undefined。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 type T = string | number | null | undefined; type Result = NonNullable<T>; // 等价于: // type Result = string | number
10. ReturnType<T>
-
作用:获取函数类型
T的返回值类型。 -
示例:
csharp 体验AI代码助手 代码解读 复制代码 function getUser() { return { name: 'Alice', age: 30 }; } type User = ReturnType<typeof getUser>; // 等价于: // type User = { // name: string; // age: number; // }
11. InstanceType<T>
-
作用:获取构造函数类型
T的实例类型。 -
示例:
typescript 体验AI代码助手 代码解读 复制代码 class User { name: string; constructor(name: string) { this.name = name; } } type UserInstance = InstanceType<typeof User>; // 等价于: // type UserInstance = User
12. Parameters<T>
-
作用:获取函数类型
T的参数类型元组。 -
示例:
typescript 体验AI代码助手 代码解读 复制代码 function greet(name: string, age: number) { console.log(`Hello, ${name}! You are ${age} years old.`); } type GreetParams = Parameters<typeof greet>; // 等价于: // type GreetParams = [string, number]
13. ConstructorParameters<T>
-
作用:获取构造函数类型
T的参数类型元组。 -
示例:
typescript 体验AI代码助手 代码解读 复制代码 class User { constructor(public name: string, public age: number) {} } type UserConstructorParams = ConstructorParameters<typeof User>; // 等价于: // type UserConstructorParams = [string, number]
14. ThisParameterType<T>
-
作用:获取函数类型
T的this参数类型。 -
示例:
typescript 体验AI代码助手 代码解读 复制代码 function greet(this: { name: string }) { console.log(`Hello, ${this.name}!`); } type GreetThis = ThisParameterType<typeof greet>; // 等价于: // type GreetThis = { name: string }
15. OmitThisParameter<T>
-
作用:移除函数类型
T的this参数类型。 -
示例:
typescript 体验AI代码助手 代码解读 复制代码 function greet(this: { name: string }) { console.log(`Hello, ${this.name}!`); } type GreetWithoutThis = OmitThisParameter<typeof greet>; // 等价于: // type GreetWithoutThis = () => void
16. Awaited<T>
-
作用:获取
Promise类型T的解析值类型。 -
示例:
ini 体验AI代码助手 代码解读 复制代码 type PromiseResult = Awaited<Promise<string>>; // 等价于: // type PromiseResult = string
小结
| 工具类型 | 作用 |
|---|---|
Partial<T> | 将类型 T 的所有属性变为可选。 |
Required<T> | 将类型 T 的所有属性变为必选。 |
Readonly<T> | 将类型 T 的所有属性变为只读。 |
Record<K, T> | 创建一个对象类型,其键为 K,值为 T。 |
Pick<T, K> | 从类型 T 中选取指定的属性 K。 |
Omit<T, K> | 从类型 T 中排除指定的属性 K。 |
Exclude<T, U> | 从类型 T 中排除可以赋值给 U 的类型。 |
Extract<T, U> | 从类型 T 中提取可以赋值给 U 的类型。 |
NonNullable<T> | 从类型 T 中排除 null 和 undefined。 |
ReturnType<T> | 获取函数类型 T 的返回值类型。 |
InstanceType<T> | 获取构造函数类型 T 的实例类型。 |
Parameters<T> | 获取函数类型 T 的参数类型元组。 |
ConstructorParameters<T> | 获取构造函数类型 T 的参数类型元组。 |
ThisParameterType<T> | 获取函数类型 T 的 this 参数类型。 |
OmitThisParameter<T> | 移除函数类型 T 的 this 参数类型。 |
Awaited<T> | 获取 Promise 类型 T 的解析值类型。 |
3、什么是泛型?
4、怎么配置tsconfig.json?
参考:
如何用 Typescript 写一个完整的 Vue 应用程序
深入 TypeScript 中的子类型,进阶 Vue3 源码前必须搞懂的。
深入 TypeScript 中的子类型、逆变、协变,进阶 Vue3 源码前必须搞懂的。
Vue3都用ts重构了,TypeScript咱也不能掉队(第二篇)
因为这几个 TypeScript 代码的坏习惯,同事被罚了 500 块
Typescript 中的 interface 和 type 到底有什么区别
开发者回避使用 TypeScript 的三个借口,以及应当使用 TypeScript 的更有说服力的原因
手摸手一起学习Typescript第六天 - 泛型 Generics / 泛型约束 / 泛型与类和接口