how to use the invoke function from the lodash library in typescript

To use the invoke function from the lodash library in TypeScript, you should first install the library:

npm install --save lodash
26 chars
2 lines

Then, you can import the invoke function from the library and use it in your TypeScript code as follows:

import * as _ from "lodash";

class MyClass {
  private myMethod(a: number, b: number) {
    return a + b;

  public run() {
    const result = _.invoke(this, "myMethod", 1, 2);
    console.log(result); // Output: 3

const myInstance = new MyClass();;
279 chars
16 lines

In this example, we import the entire lodash library using the * as _ syntax. Then, we define a class MyClass with a method myMethod that takes two parameters and returns their sum. Finally, we create an instance of MyClass, call the run method, and use the invoke function from lodash to call the myMethod method on the instance with two arguments 1 and 2. The result is logged to the console.

Note that the first argument to invoke is the object on which the method should be invoked (in this case, this refers to the instance of MyClass). The second argument is the name of the method to be invoked, and the rest of the arguments are the arguments to be passed to the method.

gistlibby LogSnag