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

How can I extract a type from a union of (Type | Type[]) to get Type?

I’m trying to get the type of an exported union type by the array of it inside as represented in the playground above. What would be the best way to do this without using the Type itself (as it is non-exported)?

interface CurrentType {
  a: string;
  b: number;
}

type ExportedType = CurrentType | CurrentType[]

type DesiredType = CurrentType

// How can I get to DesiredType from CurrentType?

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgMIFcpQuAKgTwAcUBvAWAChlk4AuZAZzClAHMBuS6gI3pHQC23aJwoBfSpTBEUAUQAehAPZRIAEwLFkAXjSZseGcgA+erDjCaIAbQC6kitK0ARCA2DYNR3RnOHiDgD0gcgAEkoA7sgIcCDIAJLIrBBgyGBKyK7unlbIMFBKAmYGljIA-EA

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 :

Exclude arrays of any type from the union

type DesiredType = Exclude<ExportedType, Array<any>>
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