top | item 35192671

(no title)

NoInkling | 3 years ago

> there is currently no sensible way to type a non empty array in Typescript

If we remove the "sensible" requirement:

    type NonEmptyArray = [any, ...any[]]

discuss

order

triyambakam|3 years ago

Or better:

    type NonEmptyArray = [unknown, ...unknown[]];
    const nea: NonEmptyArray = [];
    //    ^^^ Type '[]' is not assignable to type 'NonEmptyArray'.
    //        Source has 0 element(s) but target requires 1.

throwaway09432|3 years ago

Can you map over this and preserve the NonEmptyArray type?