Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Construct a union type from an object type

I have a type that looks like this:

type MessageTypes = {
  foo: { bar: string };
  baz: { qux: string, quux: string };
  corge: { grault: number };
};

I’d like to define a type that gives me something like:

| {
  op: 'foo';
  data: { bar: string };
}
| {
  op: 'baz';
  data: { qux: string, quux: string };
}
| {
  op: 'corge';
  data: { grault: number };
}

The closest I’ve come so far is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

type KnownMessage = {
  [T in keyof MessageTypes]: { op: T; data: MessageTypes[T] };
};

However, I don’t care about the keys from this type, only the values.

What’s the best way to define my special union type using MessageTypes?

>Solution :

Very close; just get a union of all the values like this:

type KnownMessage = {
  [T in keyof MessageTypes]: { op: T; data: MessageTypes[T] };
}[keyof MessageTypes];

It works because this:

{
    foo: 1,
    bar: 2,
}["foo" | "bar"]

results in a union of the values 1 and 2 => 1 | 2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading