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