Type description + examples
Super simple type, it takes one generic and returns a union of it or a function that returns it.
import type { Promisable, Thunkable } from "type-fest"
type Config = {
inputValue: Thunkable<Promisable<string>>
}
export async function exec(config: Config) {
const value: string = await (typeof config.inputValue === "function" ? config.inputValue() : config.inputValue)
}
No real opinion on the naming, just not sure what else to call it.
When authoring a custom plugin for various build tools (ex. vite), the plugin lifecycle callback functions are typically allowed to be sync or async. If a plugin author wanted to extend that to the plugin consumers via the plugin's input options, that would allow the plugin consumer to delay certain operations until the plugin actually needs it.
Ex. If there's an input that would require the caller to import from another module, and using ESM they could prefer a dynamic import which would only be executed when the plugin calls the thunk. otoh, a different consumer might want to pass a static value.
Type source
/**
* A value or a function that returns the value
*/
export type Thunkable<T> = T | (() => T);
Search existing types and issues first
Type description + examples
Super simple type, it takes one generic and returns a union of it or a function that returns it.
No real opinion on the naming, just not sure what else to call it.
When authoring a custom plugin for various build tools (ex. vite), the plugin lifecycle callback functions are typically allowed to be sync or async. If a plugin author wanted to extend that to the plugin consumers via the plugin's input options, that would allow the plugin consumer to delay certain operations until the plugin actually needs it.
Ex. If there's an input that would require the caller to import from another module, and using ESM they could prefer a dynamic import which would only be executed when the plugin calls the thunk. otoh, a different consumer might want to pass a static value.
Type source
Search existing types and issues first