Zachu
Zachu

Reputation: 47

Get typeof type from type with classes

Currently I have 2 types like this:

export type Component = AComponent| BComponent| CComponent;
export type ComponentType= typeof AComponent| typeof BComponent| typeof CComponent;

Instead of duplicating code like this, is there a way to write my components once, and get two types similar to this?

Thank you

Upvotes: 1

Views: 27

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249756

You can use InstanceType on the ComponentType union (Assuming *Component are classes). InstanceType is distributive, so you will get the same union as Component

export type ComponentType= typeof AComponent| typeof BComponent| typeof CComponent;
export type Component =InstanceType<ComponentType> // AComponent | BComponent | CComponent

Playground Link

Upvotes: 2

Related Questions