Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Thunkable - a direct value or function which returns that value #970

Open
1 task done
JQuezada0 opened this issue Oct 21, 2024 · 0 comments
Open
1 task done

Comments

@JQuezada0
Copy link

JQuezada0 commented Oct 21, 2024

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

  • I tried my best to look for it

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant