1

Is there a way in Typescript to get a union of all the types defined inside an interface?

So for example if I have an interface:

interface Events {
    connect: () => void;
    message: (data: object) => void;
}

I can get a union of its keys by using keyof Events which will yield "connect" | "message". Is there some equivalent way of getting a union of allowed types, which would yield (() => void) | ((data: object) => void)?

echus
  • 51
  • 5
  • 1
    You use an [indexed access](https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html) type along with `keyof`, like `Events[keyof Events]`. See the answer to the linked question for more information – jcalz Jul 14 '22 at 02:34

0 Answers0