You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript递归类型提示问题:PopulationDto嵌套路径推导失效

解决TypeScript递归DTO的类型提示问题

问题分析

当前PopulationDto的递归字段population使用any类型,导致嵌套的path无法根据上级选择的属性自动推断对应类型范围。核心问题是未建立path与嵌套population的类型关联,也未处理数组类型属性的元素提取(比如User中的address是Address[],需要提取元素类型作为嵌套DTO的泛型参数)。

解决方案

1. 定义辅助类型处理数组元素提取

创建ElementType类型,用于提取数组的元素类型,非数组则直接返回原类型:

type ElementType<T> = T extends (infer U)[] ? U : T;

2. 重构递归类型关联path与嵌套类型

替换原PopulationDto为递归联合类型PopulationItem,让每个path对应的嵌套population类型自动关联到该属性的实际类型(已处理数组):

type PopulationItem<E> = {
  [K in keyof E]: {
    path: K;
    population?: PopulationItem<ElementType<E[K]>>[];
  };
}[keyof E];

3. 使用重构后的类型

定义population变量时,直接使用PopulationItem<User>[]作为类型:

class Building {
  floor: string;
  room: string;
}

class Address {
  building: Building;
  street: string;
  city: string;
}

class User {
  name: string;
  address: Address[];
}

const population: PopulationItem<User>[] = [
  { path: "address", population: [{ path: "building" }] },
];

效果验证

  • 第一个path会提示"address" | "name",符合预期;
  • 选择path: "address"后,嵌套population中的path会自动提示"building" | "street" | "city";
  • 若选择path: "name",由于name是string类型,其keyof string为never,此时population字段会被约束为不可定义(无有效path可选),避免无效类型输入。

可选:保留类形式的DTO

若需保留PopulationDto类结构,可基于PopulationItem类型约束类的成员:

class PopulationDto<E> implements PopulationItem<E> {
  path!: keyof E;
  population?: PopulationItem<ElementType<E[keyof E]>>[];
}

注:类泛型无法自动关联path与population的具体类型,推荐直接使用PopulationItem类型以获得更精准的提示。

内容的提问来源于stack exchange,提问作者Tan Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 14:32:45