获取对象中值的并集

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

Get union of values in for object

问题

I can help with that. Here's the translation of your provided content:

我尝试编写一个函数的类型,该函数接受任何结构的记录并将值转换为一个可辨识联合类型。所以类似以下的东西:

const getKeys = <T extends { key: string }>(items: T[]): T['key'] => {
    // ...
}

// keys 应该具有类型 "foo" | "bar"
// 实际上具有 string 类型
const keys = getKeys([{ key: "foo" }, { key: "bar" }])

// keys2 应该具有类型 "baz" | "qux"
// 实际上具有 string 类型
const keys2 = getKeys([{ key: "foo" }, { key: "qux" }])

然而,keyskeys2 实际上具有 string 类型。

理想情况下,您可以获得类似以下的良好 API,使用记录的工作示例:

const getKeys = <T extends string>(items: T[]): T => {
    // ...
}

// keys 具有类型 "foo" | "bar"
const keys = getKeys(["foo", "bar"])

// keys2 具有类型 "baz" | "qux"
const keys2 = getKeys(["baz", "qux"])

如何实现这个?要求不能在参数上使用 as const

英文:

I'm trying to write the types for a function that accepts records of any structure and transforms the values into a discriminated union. So something like the following:

const getKeys = &lt;T extends {key: string}&gt;(items: T[]): T[&#39;key&#39;] =&gt; {
    // ...
}

// keys should have type &quot;foo&quot; | &quot;bar&quot;
// instead has type string
const keys = getKeys([{ key: &quot;foo&quot; }, { key: &quot;bar&quot; }])

// keys2 should have type &quot;baz&quot; | &quot;qux&quot;
// instead has type string
const keys2 = getKeys([{ key: &quot;foo&quot; }, { key: &quot;qux&quot; }])

However, keys and keys2 instead has the type of string.

Ideally, you would get the kind of nice API like the following, working example but using records:

const getKeys = &lt;T extends string&gt;(items: T[]): T =&gt; {
    // ...
}

// keys has type &quot;foo&quot; | &quot;bar&quot;
const keys = getKeys([&quot;foo&quot;, &quot;bar&quot;])

// keys2 has type &quot;baz&quot; | &quot;qux&quot;
const keys2 = getKeys([&quot;baz&quot;, &quot;qux&quot;])

How can I do this?

Requirements

  • Cannot use as const on the argument

答案1

得分: 2

The alternative to const assertion would be const type parameters introduced in Typescript 5.0.

Previously, typescript would infer a more generic type for the objects; however, const type assertions tells the compiler to take the exact type without making it more general.

Implementation:

const getKeys = <const T extends { key: string }>(items: T[]): T['key'] => {
  return {} as any
};

// keys should have type "foo" | "bar"
const keys = getKeys([{ key: 'foo' }, { key: 'bar' }]);

// keys2 "baz" | "qux"
const keys2 = getKeys([{ key: 'bar' }, { key: 'qux' }]);

playground

英文:

The alternative to const assertion would be const type parameters introduced in Typescript 5.0.

Previously, typescript would infer a more generic type for the objects; however, const type assertions tells the compiler to take the exact type without making it more general.

Implementation:

const getKeys = &lt;const T extends { key: string }&gt;(items: T[]): T[&#39;key&#39;] =&gt; {
  return {} as any
};

// keys should have type &quot;foo&quot; | &quot;bar&quot;
const keys = getKeys([{ key: &#39;foo&#39; }, { key: &#39;bar&#39; }]);

// keys2 &quot;baz&quot; | &quot;qux&quot;
const keys2 = getKeys([{ key: &#39;bar&#39; }, { key: &#39;qux&#39; }]);

playground

huangapple
  • 本文由 发表于 2023年5月21日 05:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297509.html
匿名

发表评论

匿名网友

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

确定