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

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');