4 ms·
> there is currently no sensible way to type a non empty array in Typescript If we remove the "sensible" requirement: type NonEmptyArray = [any, ...any[]]
by NoInkling 4y 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[]]
- triyambakam 4y agoOr 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 4y agoCan you map over this and preserve the NonEmptyArray type?
- triyambakam 4y agoDo you mean pass in a type? type NonEmptyArray<T> = [T, ...T[]];
- throwaway09432 4y agoI mean if I have a NonEmptyArray say xs, and I then xs.map(x=>2*x), will the result of this expression be a NonEmptyArray ?
- triyambakam 4y agoNo, unfortunately it will just be number[] as the return type.