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

Typescript array type extends a base array type

I am looking for a way to extend the type of an array, ie having at least the base array elements and possibly other elements.

use case: to communicate with translations APIs, I defined a base languages array:

const languages = ['FR', 'EN'] as const;
type BaseLanguages = typeof languages;

The objective is to define some derived types, arrays which contains at least ‘FR’ and ‘EN’ and possibly other string languages.

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

Thanks for your help

>Solution :

Maybe the spread operator can you help you here.

const languages = ['FR', 'EN'] as const;
type BaseLanguages = [...typeof languages, ...string[]];

Playground

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