Skip to content

Instantly share code, notes, and snippets.

@aryelgois
Last active August 22, 2022 19:22
Show Gist options
  • Save aryelgois/3ee33a135848a12635a0726da1b740ce to your computer and use it in GitHub Desktop.
Save aryelgois/3ee33a135848a12635a0726da1b740ce to your computer and use it in GitHub Desktop.
type UnaryFn<T, R> = (param: T) => R
export function maybeCall<T, R>(fn: UnaryFn<T, R>): UnaryFn<T | null, R | null>;
export function maybeCall<T, R>(fn: UnaryFn<T, R>, value: T | null): R | null;
export function maybeCall<T, R>(fn: UnaryFn<T, R>, value?: T | null) {
if (value === undefined) {
return (v: T | null) => maybeCall(fn, v)
}
return value !== null ? fn(value) : null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment