3

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.

2
  • 3
    That's not an intersection. 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. Dec 20, 2020 at 0:07

1 Answer 1

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.