admin管理员组

文章数量:1023815

How to transform union of similar objects to object type using TypeScript typings?

Input

type I = {
  key: 'foo',
  value: Foo,
} | {
  key: 'bar',
  value: Bar,
};

Output

type O = {
  foo: Foo,
  bar: Bar,
};

I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.

How to transform union of similar objects to object type using TypeScript typings?

Input

type I = {
  key: 'foo',
  value: Foo,
} | {
  key: 'bar',
  value: Bar,
};

Output

type O = {
  foo: Foo,
  bar: Bar,
};

I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.

Share Improve this question edited Dec 20, 2020 at 1:18 jcalz 333k29 gold badges443 silver badges442 bronze badges asked Dec 19, 2020 at 23:52 avdotionavdotion 1371 silver badge10 bronze badges 2
  • 3 That's not an intersection. – Jared Smith Commented Dec 20, 2020 at 0:01
  • It's not clear what the algorithm would even be to get from your "input" to your "output" unless you had a type that happened to be a union of a bunch of types with key properties of type string and value properties of various types. And how would you use this output? You want an expression that produces O from I? How does that help you? O is not that hard to type into your code -- you did it above. So yeah, see whether you can improve or clarify the question, I'd say. – David P. Caldwell Commented Dec 20, 2020 at 0:07
Add a ment  | 

1 Answer 1

Reset to default 6

In TypeScript 4.1 this is a straightforward mapped type with key remapping. You can iterate over each member T of the I union (it's a union, not an intersection) and look up the key/value types: T['key'] for the key and T['value'] for the value. Like this:

type O = { [T in I as T['key']]: T['value'] };
/* type O = {
    foo: Foo;
    bar: Bar;
} */

Playground link to code

How to transform union of similar objects to object type using TypeScript typings?

Input

type I = {
  key: 'foo',
  value: Foo,
} | {
  key: 'bar',
  value: Bar,
};

Output

type O = {
  foo: Foo,
  bar: Bar,
};

I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.

How to transform union of similar objects to object type using TypeScript typings?

Input

type I = {
  key: 'foo',
  value: Foo,
} | {
  key: 'bar',
  value: Bar,
};

Output

type O = {
  foo: Foo,
  bar: Bar,
};

I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.

Share Improve this question edited Dec 20, 2020 at 1:18 jcalz 333k29 gold badges443 silver badges442 bronze badges asked Dec 19, 2020 at 23:52 avdotionavdotion 1371 silver badge10 bronze badges 2
  • 3 That's not an intersection. – Jared Smith Commented Dec 20, 2020 at 0:01
  • It's not clear what the algorithm would even be to get from your "input" to your "output" unless you had a type that happened to be a union of a bunch of types with key properties of type string and value properties of various types. And how would you use this output? You want an expression that produces O from I? How does that help you? O is not that hard to type into your code -- you did it above. So yeah, see whether you can improve or clarify the question, I'd say. – David P. Caldwell Commented Dec 20, 2020 at 0:07
Add a ment  | 

1 Answer 1

Reset to default 6

In TypeScript 4.1 this is a straightforward mapped type with key remapping. You can iterate over each member T of the I union (it's a union, not an intersection) and look up the key/value types: T['key'] for the key and T['value'] for the value. Like this:

type O = { [T in I as T['key']]: T['value'] };
/* type O = {
    foo: Foo;
    bar: Bar;
} */

Playground link to code

本文标签: javascriptTypescript Convert union of similar objects to object typeStack Overflow