4

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. 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. Commented Dec 20, 2020 at 0:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.