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.
/**
* A value or a function that returns the value
*/
export type Thunkable<T> = T | (() => T);
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too