我的目标是,对于包含Nested
个通用的给定类型
type Example = { a: string, b: string, n: Nested<{ n1: 'n1-val', n2: 'n2-val' }> }
通过实施FlatNested
将公寓Nested
转化为母类型:
type t1 = FlatNested<Example>
// ^? goal: {a: string;b: string;n1: "n1-val" ;n2: "n2-val";}
当前进度
type Nested<T> = T &
{ _: string } // this just so Nested<T> wont be equal to T
type Example = { a: string, b: string, n: Nested<{ n1: 'n1-val', n2: 'n2-val' }> }
type FlatNested<T> = { [K in keyof T as T[K] extends Nested<infer N> ? keyof N : K]: T[K] extends Nested<infer N> ? T[K][keyof N] : T[K] };
type t1 = FlatNested<Example>
// ^? type t1 = {a: string;b: string;n1: "n1-val" | "n2-val";n2: "n1-val" | "n2-val";}
// ^? goal: {a: string;b: string;n1: "n1-val" ;n2: "n2-val";}
我已经很接近了,但还没有到那里,我无法分离嵌套类型中键的值.无论我try 什么,我最终都会得到所有值的联合,而不是与当前重命名的映射类型(as
后面的)相匹配的值.