Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created January 21, 2026 02:16
Show Gist options
  • Select an option

  • Save jonathanconway/62330de7be683c4cc478c20467153a87 to your computer and use it in GitHub Desktop.

Select an option

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
// 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