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

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

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

>Solution :

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

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