Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a non empty array type in Typescript?

Tags:

typescript

I have a list of numbers that I know is never empty. Is it possible to define an array in Typescript that is never empty?

I know that it is possible with tuples like [ number, number ] but this will not work as my array can be any size.

I guess what I am looking for is a NonEmptyArray<number> type.

Does it exist? :)

like image 842
mspoulsen Avatar asked May 06 '19 13:05

mspoulsen


People also ask

What is non empty array?

An array that always contains at least one element. Additionaly, it can track a currently selected index, which is guaranteed to point to an existing element in the array.

What is the type of empty array in TypeScript?

To declare an empty array for a type variable, set the array's type to Type[] , e.g. const arr: Animal[] = [] . Any elements you add to the array need to conform to the specific type, otherwise you would get an error.

What is array type in TypeScript?

In typescript, an array is a data type that can store multiple values of different data types sequentially. Similar to JavaScript, Typescript supports array declaration and there are multiple ways to do it. Declaring and Initializing Arrays: We can either use var or let for declaring an array.

Is string empty TypeScript?

Whether you use JavaScript or TypeScript to verify a variable is an empty string ensure the type of the variable is a “string” and verify the string has a length of zero after removing any white spaces the string could have.

How do I avoid an empty array in typescript?

If we have an empty array, the type would be inferred as any [] implicitly. To avoid this, we should specify the type of it explicitly. This applies if strictNullChecks is set to false . When null and undefined aren’t assignable to other types, Typescript infers to empty arrays differently.

Why does typescript have never type instead of never?

When null and undefined aren’t assignable to other types, Typescript infers to empty arrays differently. It’ll have the never type instead of any [] if strictNullChecks is set to true .

How do I declare an empty array of a specific type?

Alternatively, you can use a type assertion to declare an empty array of a specific type. When using type assertions, we are effectively telling TypeScript that we know better and the arr variable is definitely of type Animal [].

How to specify array data type in typescript?

We can specify the array data type with brackets. For instance, we can write: If the data type is obvious from the values, then we don’t have to write the data type annotation explicitly. For instance, we can write: The TypeScript compiler will know that we have a number array just from the assignment.


2 Answers

A feature request for allowing you to just check array.length > 0 to guard against empty arrays, microsoft/TypeScript#38000, was declined as being too complex. Essentially you cannot usually simply check length in TypeScript to convince the compiler about the availability of properties at given numeric keys.

You can define a non-empty array type like this:

type NonEmptyArray<T> = [T, ...T[]];  const okay: NonEmptyArray<number> = [1, 2]; const alsoOkay: NonEmptyArray<number> = [1]; const err: NonEmptyArray<number> = []; // error! 

This is due to support added in TS 3.0 for rest elements in tuple types. I'm not sure what your use case is... It's probably more annoying to use that type than you expect, though:

function needNonEmpty(arr: NonEmptyArray<number>) {} function needEmpty(arr: []) {}  declare const bar: number[]; needNonEmpty(bar); // error, as expected  if (bar.length > 0) {     needNonEmpty(bar); // ugh, still error! } 

If you want a length check to work, you'll need to use something like a user-defined type guard function, but it's still annoying to use:

function isNonEmptyArray<T>(arr: T[]): arr is NonEmptyArray<T> {     return arr.length > 0; }  if (isNonEmptyArray(bar)) {     needNonEmpty(bar); // okay } else {     needEmpty(bar); // error!! urgh, do you care?         }  

Anyway hope that helps. Good luck!

like image 122
jcalz Avatar answered Sep 20 '22 15:09

jcalz


I've also wondered about this and came up with a different solution:

type NonEmptyArray<T> = T[] & { 0: T }; 
like image 30
Andrei Vajna II Avatar answered Sep 20 '22 15:09

Andrei Vajna II