Typescript: Declare functions without implementing them

随声附和 提交于 2021-02-10 03:09:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!