MST
星途 面试题库

面试题:TypeScript深度逻辑类型推导与递归应用

实现一个TypeScript类型 `DeepPartial`,它可以将一个复杂对象类型(可能包含嵌套对象)的所有属性变为可选。例如,对于类型 `{a: {b: string; c: number}, d: boolean}`,`DeepPartial<{a: {b: string; c: number}, d: boolean}>` 应返回 `{a?: {b?: string; c?: number}; d?: boolean}`。要求使用类型编程和递归实现,尽可能优雅和通用。
27.2万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
type DeepPartial<T> = {
    [P in keyof T]?: T[P] extends object
      ? DeepPartial<T[P]>
       : T[P];
};