rest()
rest<
Result>(func,start?): (...args) =>Result
Creates a function that invokes func with the this binding of the created function and an array of arguments.
DEPRECATED
Use rest parameters (...args) syntax directly instead.
Type Parametersβ
Result: Resultβ
The return type of the function.
Parametersβ
func: (...args) => Resultβ
The function to apply a rest parameter to.
start?: numberβ
The start position of the rest parameter. Defaults to func.length - 1.
Returnsβ
The new function.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
rest (Lodash, es-toolkit) Β· β (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleβ
// β Deprecated approach
const say = rest((what: string, names: string[]) => {
return what + ' ' + names.join(', ');
});
say('hello', 'fred', 'barney', 'pebbles');
// => 'hello fred, barney, pebbles'
// β
Recommended approach
const say = (what: string, ...names: string[]) => {
return what + ' ' + names.join(', ');
};
say('hello', 'fred', 'barney', 'pebbles');
// => 'hello fred, barney, pebbles'
How it works?β
Creates a function that collects arguments into array. Deprecated: Use rest parameters directly.
Native Equivalentβ
// β rest(fn)
// β
(...args) => fn(args)
Use Casesβ
Collect remaining arguments πβ
Collect remaining arguments into array.
const log = (first, ...rest) => {
console.log(first, rest);
};
log(1, 2, 3, 4); // => 1, [2, 3, 4]
Forward argumentsβ
Pass through all but first arguments.
const wrapper = (fn, ...args) => fn(...args);
Variadic functionsβ
Accept any number of arguments.
const sum = (...nums) => nums.reduce((a, b) => a + b, 0);
sum(1, 2, 3, 4); // => 10