Skip to main content

invoke()

invoke<T>(object, path, args): unknown

Invokes the method at path of object with the given arguments.

DEPRECATED

Use get(obj, path)?.(...args) or optional chaining directly instead.


Type Parametersโ€‹

T: Tโ€‹


Parametersโ€‹

object: Tโ€‹

The object to query.

path: string | (string | number | symbol)[]โ€‹

The path of the method to invoke.

args: unknown[] = []โ€‹

The arguments to invoke the method with.


Returns: unknownโ€‹

The result of the invoked method.


See Alsoโ€‹

get


Sinceโ€‹

2.0.0


Also known asโ€‹

invoke (Lodash, Antfu) ยท โŒ (es-toolkit, Remeda, Radashi, Ramda, Effect, Modern Dash)


Exampleโ€‹

const object = {
a: {
b: {
greet: (name: string) => `Hello, ${name}!`
}
}
};

// โŒ Deprecated approach
invoke(object, 'a.b.greet', ['World']);
// => 'Hello, World!'

// โœ… Recommended approach
get(object, 'a.b.greet')?.('World');
// => 'Hello, World!'

// Or with optional chaining
object.a?.b?.greet?.('World');
// => 'Hello, World!'

How it works?โ€‹

Invokes method at path with given arguments.

Processing Flowโ€‹

Common Inputsโ€‹

ObjectPathArgsResult
{greet: n => 'Hi ' + n}'greet'['Bob']'Hi Bob'
{a: 1}'a'[]undefined

warning Deprecated: Use get(obj, path)?.(...args) or optional chaining.


Use Casesโ€‹

Call method at path ๐Ÿ“Œโ€‹

Invoke method at nested path.

import { get } from '@pithos/arkhe';
const method = get(obj, 'a.b.greet');
const result = typeof method === 'function' ? method('World') : undefined;

Optional method callโ€‹

Safely call method if exists.

obj.a?.b?.greet?.('World');