简体   繁体   English

使用环境声明的类型化函数说:不是函数

[英]typed function using ambient declaration says: is not a function

Here is the situation: 情况如下:

I have an old third party javascript function and I want to use it in my typescript codes simply using ambient declaration: 我有一个旧的第三方javascript函数,我想仅使用环境声明在我的打字稿代码中使用它:

old.js: (is loaded by <script> in my .aspx file)
function myFunc() {
    return “Hello World”;
}

typing.d.ts:
export declare function myFunc(): string;

app.ts
import { myFunc } from “./typing”
let str: string = myFunc()

transpild app.js:
var str = typing_1.myFunc();

There is no issue in compile time but in runtime I get this error: 'myFunc is not a function' 编译时没有问题,但在运行时出现此错误: “ myFunc不是函数”

Any help will be appreciated 任何帮助将不胜感激

So many appreciate @AluanHaddad for his comments toward fixing the issue. 非常感谢@AluanHaddad对解决此问题的评论。 Here is the answer: 答案是:

Need to rename typing.d.ts to old.d.ts and put it in the same folder that old.js resides. 需要将type.d.ts重命名为old.d.ts,并将其放在old.js所在的文件夹中。 This is due to the old.js just includes global and doesn't have any export 这是由于old.js仅包含global而没有任何导出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM