简体   繁体   中英

Is it possible to define a non empty array type in 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? :)

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!

我也想知道这个并想出了一个不同的解决方案:

type NonEmptyArray<T> = T[] & { 0: T };

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 you'll unlikely to see NonEmptyArray in practice. The above replies should have mention this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM