Created
January 21, 2026 02:16
-
-
Save jonathanconway/62330de7be683c4cc478c20467153a87 to your computer and use it in GitHub Desktop.
Type and related utilities for dealing with nil or "falsy" values in Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Type and related utilities for dealing with nil or "falsy" values in Typescript | |
| // Credits: | |
| // • You Don't Know Javascript by Kyle Simpson [Book] | |
| // • Javascript – The Good Parts by Douglas Crockford [Book] | |
| /** | |
| * Value which behaves the same as `false` when evaluated. | |
| */ | |
| export type Nil = undefined | null | false | 0 | -0 | typeof NaN | ""; | |
| /** | |
| * Value which might be either T or `Nil`. | |
| */ | |
| export type Maybe<T> = T | Nil; | |
| /** | |
| * Returns Boolean indicating whether `input` is `Nil`. | |
| */ | |
| export function isNil<T>(item?: Maybe<T>): item is Nil { | |
| return !Boolean(item); | |
| } | |
| /** | |
| * Returns Boolean indicating whether `input` is not `Nil`. | |
| */ | |
| export function isNotNil<T>(item?: Maybe<T>): item is T { | |
| return Boolean(item); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment