Last active
August 22, 2022 19:22
-
-
Save aryelgois/3ee33a135848a12635a0726da1b740ce to your computer and use it in GitHub Desktop.
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 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