Home > front end >  How to define array length and elements types using the Array interface in TypeScript
How to define array length and elements types using the Array interface in TypeScript

Time:11-08

What I want to achieve can be done like bellow:

// Will define a type that is an array with TWO elements where the first will be a string
// and the second a number
type SomeArrayType = [string, number];

I did not like to use this kind of type declaration for arrays and I like to use the Array interface for this, like:

// It will define a type that is an array of string with any number of elements in it
type AnotherArrayType = Array<string>;

But since the Array interface only takes one type argument and there is no explicit way to define the length, is there a way to achieve it using the interface or I really gonna have to define my type using the first approach?

Thanks

  • Related