S
S
Sergey Erzhovich2022-03-01 12:40:15
typescript
Sergey Erzhovich, 2022-03-01 12:40:15

What is the best way to make a type for a callback with this passing to it?

Tell me the type for the callback, so that there would be a highlight of the methods

class BaseLol {
  myMethod1(arg = 123) {
    return arg;
  }
  public extend(callback: CallableFunction) {
    callback(this);
    return this;
  }
    /* ... */
}

class Lol extends BaseLol {
  myMethod2(arg = "qwe") {
    return arg;
  }
}

const instance = new Lol();

instance.extend((inst) => { //<<< нужен тип что бы узнать какие пропсы, методы и тд...
        inst.customProp = 'lol';
  inst.myMethod1(123123);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Yerzhovich, 2022-03-01
@Drilled-prog

public extend(callback: (inst: this) => void) {
    callback(this);
    return this;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question