问题
Is it possible to declare that a class has certain functions without implementing them?
I need this because I create a class that is then passed to a js framework which adds a couple of functions. I wanna be able to call those functions aswell without reimplementing them, redirecting or casting or whatsoever.
Example:
//example class
class AppComponent {
constructor() {}
}
//create class
var comp: AppComponent = new AppComponent();
//hand over to framework
fw.define("Component", comp);
//now I want to be able to call the added functions such as:
comp.setModel(/*some model*/);
回答1:
You may declare the function without implementing it.
//example class
class AppComponent {
constructor() { }
setModel: () => void;
}
//create class
var comp: AppComponent = new AppComponent();
//hand over to framework
fw.define("Component", comp);
//now I want to be able to call the added functions such as:
comp.setModel(/*some model*/);
来源:https://stackoverflow.com/questions/44653623/typescript-declare-functions-without-implementing-them