Home > Software engineering >  Typescript array type extends a base array type
Typescript array type extends a base array type

Time:07-12

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.

Thanks for your help

CodePudding user response:

Maybe the spread operator can you help you here.

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

Playground

  • Related