Skip to content

Instantly share code, notes, and snippets.

@dperuo
Created May 8, 2024 15:34
Show Gist options
  • Save dperuo/91b8952978421f14d5907fff9465ab4a to your computer and use it in GitHub Desktop.
Save dperuo/91b8952978421f14d5907fff9465ab4a to your computer and use it in GitHub Desktop.
/**
* Returns the result of calling the function (if it is a function) or the value itself.
*
* @example
*
* ```ts
* callable(() => true)
* // => true
*
* callable(false)
* // => false
* ```
*/
export const callable = <T>(value: T) =>
(typeof value === 'function' ? value() : value) as T extends (...args: unknown[]) => infer R ? R : T;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment