74

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? :)

4
  • do you mean like items required? May 6, 2019 at 13:22
  • It seems like one should be able to use Exclude for this, but although type NonEmptyNumberArray = Exclude<number[], []>; parses, the result is just an alias for number[]. May 6, 2019 at 13:38
  • I mean like type MyArray = [ number, number ]. Just for n items instead of only two.
    – mspoulsen
    May 6, 2019 at 13:40

4 Answers 4

121

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?        
}
4
  • Yeah, basically use a type guard before you call it. Must be what the OP wants so it can be enforced by the compiler. Really cool and weird, I hope I never need this :) May 6, 2019 at 14:03
  • 3
    My use case is this: when using functions like head() and last() on a list of T they as default return T | undefined. Which means that I have to make a check or in cases when I know that the array is never empty I have to typecast. If I instead have a NonEmptyArray<T> my head and last functions could return T always and neither checks nor typecasting will be necessary.
    – mspoulsen
    May 7, 2019 at 7:28
  • That's great but you will still probably have to make those checks/assertions before you call head() and last() instead of during or after.
    – jcalz
    May 7, 2019 at 13:34
  • Even after using these type guards, the Array<T> return types for pop() or shift() doesn't change, so you either have to cast the result anyway, or use your own functions as in the example above. Any built in array methods wouldn't get better return types inferred.
    – Ran Lottem
    Apr 20, 2020 at 6:23
37

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

type NonEmptyArray<T> = T[] & { 0: T };
6
  • 7
    A further alternative in this style is type NonEmpty<T> = T extends Array<infer U> ? U[] & {'0': U} : never;. Usage: const x: NonEmpty<string[]> = ["myString"];. This separates the NonEmpty part from the Array part.
    – Remirror
    Sep 11, 2020 at 17:51
  • 1
    This doesn't cover arrays where the first member is undefined, but others have values.
    – jsejcksn
    Jun 4, 2021 at 8:42
  • @jsejcksn Can you give an example? Jul 7, 2021 at 8:11
  • 1
    I see arr as having the type NonEmptyArray<number | undefined> in that case. Depends on how you define the type you actually want to use. Aug 20, 2021 at 7:27
  • 5
    This is essentially the definition used by fp-ts.
    – cyberixae
    Sep 10, 2021 at 7:17
7

Note for a happy reader. None of the above solutions is viable.

type NonEmptyArray<T> = T[] & { 0: T }
// type NonEmptyArray<T> = [T, ...T[]] -- same behavior

// ISSUE 1: map does not preserve Non-Emptiness
const ns: NonEmptyArray = [1]
const ns2 = ns.map(n => n) // number[] !!!

// ISSUE 2: length check does not make an array Non-Empty
function expectNonEmpty<T>(ts: NonEmptyArray<T>): any {}

if (ns2.length > 0) {
  expectNonEmpty(ns2) // type error
}

Just FYI, so you know why it's unlikely to see NonEmptyArray in practice. The above replies should have mention this.

1

Although type [T, ...T[]] cover most cases, it won't accept arrays like [...anotherArray, 'element']. I personally use type as presented below, to cover wider spectrum of cases:

export type NonEmptyArray<T> = [T, ...T[]] | [...T[], T] | [T, ...T[], T];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.