es-toolkit/docs/reference/function/partial.md
2024-08-11 15:32:20 +09:00

1.1 KiB

partial

Creates a function that invokes func with partialArgs prepended to the arguments it receives.

This method is like bind except it does not alter the this binding.

The partial.placeholder value, which defaults to a symbol, may be used as a placeholder for partially applied arguments.

Note: This method doesn't set the length property of partially applied functions.

Signature

function partial<F extends Function>(func: F, ...partialArgs: any[]): F;

namespace partial {
  placeholder: symbol;
}

Parameters

  • func (F): The function to partially apply arguments to.
  • partialArgs (any[], optional): The arguments to be partially applied.

Returns

(F): Returns the new partially applied function.

Examples

import { partial } from 'es-toolkit/function';

function greet(greeting, name) {
  return greeting + ' ' + name;
}

const sayHelloTo = partial(greet, 'hello');
sayHelloTo('fred');
// => 'hello fred'

// Partially applied with placeholders.
const greetFred = partial(greet, partial.placeholder, 'fred');
greetFred('hi');
// => 'hi fred'