【问题标题】:TypeScript: Get deeply nested property value using arrayTypeScript:使用数组获取深度嵌套的属性值
【发布时间】:2019-04-17 00:08:48
【问题描述】:

我想声明一个函数,它可以接受一个对象加上一个嵌套属性键数组,并将嵌套值的类型派生为函数的返回类型。

例如

const value = byPath({ state: State, path: ['one', 'two', 'three'] }); 
// return type == State['one']['two']['three']

const value2 = byPath({ state: State, path: ['one', 'two'] });
// return type == State['one']['two']

我能总结的最好的是以下,但它比我想要的更冗长,我必须为每个嵌套级别添加函数重载。

export function byPath<
  K1 extends string,
  R
>({ state, path }: {
  state: {[P1 in K1]?: R},
  path: [K1]
}): R;

export function byPath<
  K1 extends string,
  K2 extends string,
  R
>({ state, path }: {
  state: {[P1 in K1]?: {[P2 in K2]?: R}},
  path: [K1, K2]
}): R;

export function byPath<
  K1 extends string,
  K2 extends string,
  K3 extends string,
  R
>({ state, path }: {
  state: {[P1 in K1]?: {[P2 in K2]?: {[P3 in K3]?: R}}},
  path: [K1, K2, K3]
}): R;

export function byPath<R>({ state, path }: { state: State, path: string[] }): R | undefined {
  // do the actual nested property retrieval
}

有没有更简单/更好的方法来做到这一点?

【问题讨论】:

  • 我认为这是一个很棒的问题。也许可以在编译期间使用扩展类型(path: [KH, ...KT])递归遍历路径数组,但不幸的是这是not supported in TS 3.1。尽管如此,我还是想出了一个未经测试的POC;如果 TS 支持元组中的其余类型,也许它会起作用。

标签: typescript


【解决方案1】:

不幸的是,TypeScript 目前不允许任意recursive type functions,这是您想要遍历键列表,深入到对象类型,并得出与列表对应的嵌套属性的类型的钥匙。你可以做它的一部分,但它是一团糟。

因此,您将不得不选择一些最高级别的嵌套并为此编写代码。这是不使用重载的函数的可能类型签名:

type IfKey<T, K> = [K] extends [keyof T] ? T[K] : T;

declare function byPath<T0,
  K1 extends keyof T0 | undefined, T1 extends IfKey<T0, K1>,
  K2 extends keyof T1 | undefined, T2 extends IfKey<T1, K2>,
  K3 extends keyof T2 | undefined, T3 extends IfKey<T2, K3>,
  K4 extends keyof T3 | undefined, T4 extends IfKey<T3, K4>,
  K5 extends keyof T4 | undefined, T5 extends IfKey<T4, K5>,
  K6 extends keyof T5 | undefined, T6 extends IfKey<T5, K6>
>({ state, path }: { state: T0, path: [K1?, K2?, K3?, K4?, K5?, K6?] }): T6;

请注意,如果需要,您可以轻松地将其扩展到六层以上的嵌套。

它的工作方式:有两种类型参数...键类型(命名为K1K2 等)和对象类型(命名为T0T1 等)。 state 属性的类型是 T0,路径是键类型的 tuple with optional elements。每个键类型要么是前一个对象类型的键,要么是 undefined。如果键未定义,则下一个对象类型与当前对象类型相同;否则它是相关属性的类型。因此,只要键类型成为并保持undefined,对象类型就会成为并保持最后一个相关的属性类型......并且最后一个对象类型(上面的T6)是函数的返回类型。

举个例子:如果T0{a: {b: string}, c: {d: string}},那么K1 必须是'a''d'undefined 之一。假设K1'a'。那么T1 就是{b: string}。现在K2 必须是'b'undefined。假设K2'b'。那么T2 就是string。现在K3 必须在keyof stringundefined 中。 (所以K3 可以是"charAt",或任何string 方法和属性)。假设K3undefined。那么T3 就是string(因为它与T2 相同)。如果K4K5K6 的所有其余部分都是undefined,那么T4T5T6 就只是string。并且函数返回T6

所以如果你这样做:

const ret = byPath({state: {a: {b: "hey"}, c: {d: "you"} }, path: ['a', 'b'] });

那么T0 将被推断为{a: {b: string}, c: {d: string}K1 将是'a'K2 将是'b',并且K3K6 都将是undefined。这是上面的例子,所以T6 将是string。因此ret 的类型为string

如果你输入了一个错误的密钥,上面的函数签名也应该对你大喊大叫:

const whoops = byPath({ state: { a: { b: "hey" }, c: { d: "you" } }, path: ['a', 'B'] });
// error! type "B" is not assignable to "b" | undefined: ----------------------> ~~~

这个错误是有道理的,因为B 无效。以下也对你大喊大叫:

const alsoWhoops = byPath({ state: { a: { b: "hey" }, c: { d: "you" } }, path: ['A', 'b'] });
// error! type "A" is not assignable to "a" | "c" | undefined: ---------------> ~~~
// also error! Type "b" is not assignable to "a" | "c" | undefined ?! -------------> ~~~

第一个错误正是您所期望的;第二个有点奇怪,因为"b" 很好。但是编译器现在不知道keyof T['A'] 会发生什么,所以它的行为就好像K1undefined。如果你修复了第一个错误,第二个就会消失。可能有办法改变byPath() 签名以避免这种情况,但对我来说似乎微不足道。


无论如何,希望对您有所帮助或给您一些想法。祝你好运!


编辑:如果您关心错误的第二条错误消息,您可以使用稍微复杂的:

type IfKey<T, K> = [K] extends [keyof T] ? T[K] : T
type NextKey<T, K = keyof any> = [K] extends [undefined] ? undefined :
  [keyof T | undefined] extends [K] ? keyof any : (keyof T | undefined)

declare function byPath<T0,
  K1 extends NextKey<T0>, T1 extends IfKey<T0, K1>,
  K2 extends NextKey<T1, K1>, T2 extends IfKey<T1, K2>,
  K3 extends NextKey<T2, K2>, T3 extends IfKey<T2, K3>,
  K4 extends NextKey<T3, K3>, T4 extends IfKey<T3, K4>,
  K5 extends NextKey<T4, K4>, T5 extends IfKey<T4, K5>,
  K6 extends NextKey<T5, K5>, T6 extends IfKey<T5, K6>
>({ state, path }: { state: T0, path: [K1?, K2?, K3?, K4?, K5?, K6?] }): T6;

这几乎是一样的,除了当键与它们应该匹配的不匹配时出现问题。

【讨论】:

  • 似乎不适用于 Partial> 类型。有什么想法吗?
  • 我最终解决了类型 IfKey = [K] extends [keyof T] ?排除, undefined> : T;然后返回 T6 的类型 extends Required ? U : T6
猜你喜欢
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 2019-04-02
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
相关资源
最近更新 更多