TypeScript面试

51 阅读18分钟

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 中排除 nullundefined

  • 示例

    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>

  • 作用:获取函数类型 Tthis 参数类型。

  • 示例

    typescript
     体验AI代码助手
     代码解读
    复制代码
    function greet(this: { name: string }) {
      console.log(`Hello, ${this.name}!`);
    }
    
    type GreetThis = ThisParameterType<typeof greet>;
    // 等价于:
    // type GreetThis = { name: string }
    

15. OmitThisParameter<T>

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

  • 示例

    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 中排除 nullundefined
ReturnType<T>获取函数类型 T 的返回值类型。
InstanceType<T>获取构造函数类型 T 的实例类型。
Parameters<T>获取函数类型 T 的参数类型元组。
ConstructorParameters<T>获取构造函数类型 T 的参数类型元组。
ThisParameterType<T>获取函数类型 Tthis 参数类型。
OmitThisParameter<T>移除函数类型 Tthis 参数类型。
Awaited<T>获取 Promise 类型 T 的解析值类型。

3、什么是泛型?

4、怎么配置tsconfig.json?

参考:

还不会TS? 带你 TypeScript 快速入门

上帝视角看 TypeScript

TypeScript中文网

亲手码出TypeScript最前沿的教程(基础篇)

亲手码出TypeScript最前沿的教程(进阶篇)

如何用 Typescript 写一个完整的 Vue 应用程序

2020年前端面试复习必读文章【超三百篇文章/赠复习导图】

如何编写 Typescript 声明文件

Typescript玩转设计模式 之 对象行为型模式(上)

TypeScript 学习资源合集

TypeScript 声明文件原理

TypeScript 期中考试现在开始

让人眼前一亮的 10 大 TS 项目

一文读懂 TypeScript 泛型及应用( 7.8K字)

TypeScript实现Map与HashMap

TypeScript 实践

TypeScript安利指南

TypeScript 高级技巧

Vue3.0 前的 TypeScript 最佳入门实践

深入 TypeScript 中的子类型,进阶 Vue3 源码前必须搞懂的。

深入 TypeScript 中的子类型、逆变、协变,进阶 Vue3 源码前必须搞懂的。

从零开始配置 TypeScript 项目

一份不可多得的 TS 学习指南(1.8W字)

细数这些年被困扰过的 TS 问题

Vue3都用ts重构了,TypeScript咱也不能掉队(第二篇)

TypeScript 高级用法

TS 常见问题整理(60多个,持续更新ing)

你不知道的 TypeScript 高级类型

因为这几个 TypeScript 代码的坏习惯,同事被罚了 500 块

Typescript 中的 interface 和 type 到底有什么区别

TypeScript的另一面:类型编程

TypeScript考试题来了,60%的人不会(附答案)

开发者回避使用 TypeScript 的三个借口,以及应当使用 TypeScript 的更有说服力的原因

手摸手一起学习Typescript第六天 - 泛型 Generics / 泛型约束 / 泛型与类和接口

探索类型系统的底层 - 自己实现一个 TypeScript(硬核干货)

TypeScript高级类型备忘录(附示例)

TypeScript 4.3 新功能的实践应用

TS 高级类型清单(附 demo)

TypeScript 声明文件全解析

TypeScript 中提升幸福感的 10 个高级技巧