MST

星途 面试题库

面试题:TypeScript条件类型推导之递归与映射类型综合运用

给定一个嵌套对象类型`type DeepObject = { a: string; b: { c: number; d: { e: boolean } } }`,实现一个条件类型`DeepMap<T, U>`,它能递归地将`T`中的所有值类型映射为`U`类型。例如`DeepMap<DeepObject, string>`应返回`{ a: string; b: { c: string; d: { e: string } } }`。
31.9万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
type DeepMap<T, U> = T extends object
  ? {
        [K in keyof T]: DeepMap<T[K], U>;
    }
  : U;

type DeepObject = { a: string; b: { c: number; d: { e: boolean } } };
type Result = DeepMap<DeepObject, string>;