Typescript深度按键选择,无需指定路径

huangapple go评论76阅读模式
英文:

Typescript deep Pick by key without specifying a path

问题

type Form = {
  collateral: {
      id: "collateralField",
      default: "",
      deps: ["collateralToken", "borrowInput", "asd"],
      severities: {
          first: 'first',
          second: 'second'
      }
  },
  borrow: {
    id: "borrowField",
    name: "Borrow"
  },
}

type IDs = DeepKeys<Form, "id">
// Expected: "collateralField" | "borrowField"

type DeepKeys<T, Q> =
  T extends object 
    ? { [K in keyof T]: (K extends Q ? T[K] : never) | DeepKeys<T[K], Q> }[keyof T]
    : never;

Typescript Playground

英文:

Let's say I've this type

type Form = {
  collateral: {
      id: &quot;collateralField&quot;,
      default: &quot;&quot;,
      deps: [&quot;collateralToken&quot;, &quot;borrowInput&quot;, &quot;asd&quot;],
      severities: {
          first: &#39;first&#39;,
          second: &#39;second&#39;
      }
  },
  borrow: {
    id: &quot;borrowField&quot;,
    name: &quot;Borrow&quot;
  },
}

I'd like to create a generic that takes the type Form and a string Query. It should return an union of all the values of the Queried keys.

type IDs = DeepKeys&lt;Form, &quot;id&quot;&gt;
// Expected: &quot;collateralField&quot; | &quot;borrowField&quot;

This is my progress

type DeepKeys&lt;T, Q&gt; =
  T extends object 
    ? { [K in keyof T]: (K extends Q ? T[K] : never) | DeepKeys&lt;T[K], Q&gt; }[keyof T]
    : never;

Typescript Playground

It seems to loop into the array object and append some unnecessary stuff.

答案1

得分: 1

你差不多搞定了。

问题在于 TypeScript 递归地迭代所有键,包括数组的键。这会在最终类型中引入不必要的键,从而造成问题。

这是一个修复:

type DeepKeys<T, Q> = T extends object 
  ? {
    [K in keyof T]: T[K] extends (infer R)[]
      ? never
      : K extends Q 
        ? T[K]
        : DeepKeys<T[K], Q>
    }[keyof T]
  : never;

刚刚在 TS Playground 上测试过,似乎工作得很好。

有了这个改进的版本,你可以这样做:

type IDs = DeepKeys<Form, "id">;
英文:

You nearly got it.

The problem is that TypeScript is recurisively iterating over all keys, including those of arrays. That creates issues as there are unncecessary keys in your final type.

Here is a fix:

type DeepKeys&lt;T, Q&gt; = T extends object 
  ? {
    [K in keyof T]: T[K] extends (infer R)[]
      ? never
      : K extends Q 
        ? T[K]
        : DeepKeys&lt;T[K], Q&gt;
    }[keyof T]
  : never;

Just tested this out and seemed to work fine on the TS playground.

With this improved version, you can do the following

type IDs = DeepKeys&lt;Form, &quot;id&quot;&gt;;

huangapple
  • 本文由 发表于 2023年7月14日 01:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定