1
let f = function () {
    this.aa += 1;
}

class A {
    constructor(f) {
        this.aa = 1;
        f.call(this);
        console.log(this.aa);
    }
}
new A(f)

how could I write a type definition for f? I want to make f only have this.aa.

Since there is this, so I guess using a class for f is more convenient. Maybe extends a base class? But how to write it?


f is defined by the user, so f can't be A's method.

AsukaMinato
  • 1,017
  • 12
  • 21
  • 3
    See [this](https://stackoverflow.com/a/28920916/5289334) answer. [Typescript playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBAYlC8UAUwAWBLAzgLigbygENDcA7AVwFsAjCAJygF8BKBAPigDcB7dAEwDcAKCEAbCMCgAzXHERTypAMbB03UslZ4hUXVDRYAdMSgBqRAEZhjEUtGFMmKAEF8OvcTJVadYXqhK6pjAdOQq3HRIMrBa7v66BpjGhAhQVnHxUoZKhKKiKBiYzH7xuoGkmNzihqLcAOYFRsTFGTY2pBAA7i5RzEA) – c0m1t Jun 07 '23 at 12:33

0 Answers0