我在使用 TypeScript 时遇到了以下问题:
有一个模块,它使用myApp.common.anotherFunc()来自“旧”js 代码的函数:
module myApp {
export module helpers {
export class baseHelper {
doWork() {
var m = myApp.common.anotherFunc();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果打字稿编译器显示错误“类型上不存在属性”。如何在不重写我的旧功能的情况下克服这个问题myApp.common?
如果重要的话,PS TS 版本是 2.0
只需为 TypeScript 声明函数:
declare module myApp.common {
let anotherFunc: Function;
}
module myApp {
export module helpers {
export class baseHelper {
doWork() {
var m = myApp.common.anotherFunc();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此声明不会生成任何代码,您将不会再看到编译器错误,因为它知道该函数。
| 归档时间: |
|
| 查看次数: |
985 次 |
| 最近记录: |