以文字对象路径作为数组进行输入

我正在寻找一种返回类型的解决方案,其中包含指向已声明对象类型的所有可能路径。

type A = {
   b: {
      c: string,},d: {
      e: string,f: string
   }
}

type ReturnPaths<T> = // here's where magic happens
type PossiblePaths = ReturnPaths<A>; // this should return ['b','c'] | ['c','d'] | ['c','e'];

这是我尝试过的:

type Join<A,B> = B extends any[] ? (((a: A,...b: B) => any) extends (...args: infer U) => any ? U : never) : never;

export type ReturnPaths<T> = {
  [K in keyof T]: K extends {} Join<K,GetFieldsPath<T[K],type>> : [K];
}[keyof T];

但是当我有更大的类型定义并返回错误时,它会卡住: Type instantiation is excessively deep and possibly infinite。同样,在以下类型的['b','e']上也不会返回错误。

因此,我正在寻找类似于将函数与条件参数一起使用的解决方案(其中每个参数都是前一个参数的键)described here,但就我而言,应该有返回此类参数数组的通用类型。

xiongming321 回答:以文字对象路径作为数组进行输入

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3108759.html

大家都在问