sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
14.21k stars 540 forks source link

`EmptyArray` type #929

Open silverwind opened 2 months ago

silverwind commented 2 months ago

There is EmptyObject, and I think a EmptyArray type for a strictly empty array would be useful.

One use case of empty array with React to avoid re-renders because of unstable default prop:

const defaultArr = [];
const Component = ({arr = defaultArr}) => arr.join(",");

Upvote & Fund

Fund with Polar

sindresorhus commented 2 months ago

You could do it with [] as const, but it could be nice to have a type for this too, for discoverability.

We already have https://github.com/sindresorhus/type-fest/blob/main/source/non-empty-tuple.d.ts

silverwind commented 2 months ago

Yeah I guess this type would mostly be for completeness sake. I guess EmptyObject has a reason for existence because of the mentioned problems surrounding the {} type that [] hopefully doesn't have.