piotrwitek / utility-types

Collection of utility types, complementing TypeScript built-in mapped types and aliases (think "lodash" for static types).
MIT License
5.54k stars 230 forks source link

Non Empty Array Type #181

Open Leandro-Albano opened 2 years ago

Leandro-Albano commented 2 years ago

Is your feature request related to a real problem or use-case?

Use-case. Where we need an non empty array to be provided.

Describe a solution including usage in code example

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

const invalid: NonEmptyArray<string> = []; // Type '[]' is not assignable to type 'NonEmptyArray<string>'. Source has 0 element(s) but target requires 1. const valid: NonEmptyArray<string> = ['']; // no error

Who does this impact? Who is this for?

Typescript users that need to type check for non empty arrays.

Describe alternatives you've considered (optional)

The alternative is to document the method/func/prop to notify the consumers that it needs a non empty array.

Additional context (optional)

Leandro-Albano commented 2 years ago

I would be happy to submit a PR for that if approved!

piotrwitek commented 2 years ago

Hey @Leandro-Albano , sorry for the late reply got some personal issues. Yes, it looks great from my perspective, please feel free to open PR and let me know to review it. Thanks