partial()
partial<
Func>(func, ...partials): (...args) =>ReturnType<Func>
Creates a function that invokes func with partials prepended to the arguments it receives.
DEPRECATED
Use arrow functions with closure or Function.bind() directly instead.
Type Parametersโ
Func: Func extends (...args) => anyโ
The type of the function to partially apply.
Parametersโ
func: Funcโ
The function to partially apply arguments to.
partials: ...any[]โ
The arguments to be partially applied.
Returnsโ
The new partially applied function.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
partial (Lodash, es-toolkit, Radashi, Ramda) ยท โ (Remeda, Effect, Modern Dash, Antfu)
Exampleโ
const greet = (greeting: string, name: string, punctuation: string) =>
`${greeting} ${name}${punctuation}`;
// โ Deprecated approach
const sayHello = partial(greet, 'Hello');
console.log(sayHello('John', '!')); // "Hello John!"
// โ
Recommended approach with bind
const sayHelloBind = greet.bind(null, 'Hello');
console.log(sayHelloBind('John', '!')); // "Hello John!"
// โ
Alternative with arrow function
const sayHelloArrow = (name: string, punctuation: string) =>
greet('Hello', name, punctuation);
console.log(sayHelloArrow('John', '!')); // "Hello John!"
How it works?โ
Creates a function with partial arguments applied.
Deprecated: Use Function.prototype.bind() or arrow functions.
Native Equivalentโ
// โ partial(fn, arg1)
// โ
fn.bind(null, arg1)
// โ
(arg2) => fn(arg1, arg2)
Use Casesโ
Pre-fill arguments ๐โ
Create function with pre-filled arguments.
const greet = (greeting, name) => `${greeting}, ${name}!`;
const sayHello = greet.bind(null, "Hello");
sayHello("Alice"); // => "Hello, Alice!"
Configure functionโ
Create configured versions of functions.
const log = (level, msg) => console[level](msg);
const logError = log.bind(null, "error");
logError("Something went wrong");
Curry first argumentโ
Fix the first parameter.
const multiply = (a, b) => a * b;
const double = multiply.bind(null, 2);
double(5); // => 10