utility-types icon indicating copy to clipboard operation
utility-types copied to clipboard

Non Empty Array Type

Open Leandro-Albano opened this issue 2 years ago • 2 comments

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 avatar Nov 06 '22 18:11 Leandro-Albano

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

Leandro-Albano avatar Nov 06 '22 18:11 Leandro-Albano

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

piotrwitek avatar Nov 14 '22 14:11 piotrwitek