打字稿:声明函数而不实现它们 [英] Typescript: Declare functions without implementing them

查看:51
本文介绍了打字稿:声明函数而不实现它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以声明一个类具有某些功能而不实现它们?

Is it possible to declare that a class has certain functions without implementing them?

我需要这个,因为我创建了一个类,然后将其传递给添加了几个函数的 js 框架.我也希望能够调用这些函数而无需重新实现它们、重定向或强制转换或其他任何东西.

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 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*/);

推荐答案

你可以声明函数而不实现它.

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*/);

这篇关于打字稿:声明函数而不实现它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆