Home > OS >  Construct a union type from an object type
Construct a union type from an object type

Time:04-30

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:

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?

CodePudding user response:

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

  • Related