sindresorhus/type-fest

Add `Truthy` and `Falsy` types

Open

#394 opened on Apr 30, 2022

View on GitHub
 (4 comments) (8 reactions) (0 assignees)TypeScript (471 forks)batch import
corehelp wantedtype addition

Repository metrics

Stars
 (12,328 stars)
PR merge metrics
 (Avg merge 3d 5h) (11 merged PRs in 30d)

Description

The implementation seems pretty straightforward:

type Falsy = Zero | "" | false | null | undefined;
type Truthy<T = Numeric | object | string | symbol | true> = Exclude<T, Falsy>;

Falsy should include NaN, but TypeScript does not provide that at the type level.

Technically, Falsy should also include HTMLAllCollection due to the [[IsHTMLDDA]] internal slot. However, that type is only available for DOM code (not NodeJS) and is deprecated anyways, so I think it's okay to exclude it from this union.

Contributor guide