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? :)
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.
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.
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.
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.
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.
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 .
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 [].
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.
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!
I've also wondered about this and came up with a different solution:
type NonEmptyArray<T> = T[] & { 0: T };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With