我有一个外部库 thing.d.ts
文件,里面有一个全局定义:
declare var thing: ThingStatic;
export default thing;
我在我的 TypeScript 中引用了 npm 模块:
import thing from 'thing';
...
thing.functionOnThing();
当我转译 TS(针对 ES6)时,它看起来像这样:
const thing_1 = require("thing");
...
thing_1.default.functionOnThing();
这会抛出一个错误:
Cannot read property 'functionOnThing' of undefined
为什么 TypeScript 在 thing_1
和 functionOnThing()
之间添加 .default
?
在 ThingStatic
上没有名为 default
的属性,在 .d. 的基础 JS 对象上也没有名为
文件定义。default
的属性。 ts
为什么 TypeScript 添加该属性,我该如何停止它?
最佳答案
import thing from 'thing';
这行代码的意思是“从模块'thing'
导入default
导出并将其绑定(bind)到本地名称东西
”。
TypeScript 按照您的要求执行并访问模块对象的 default
属性。
你可能想写的是
import * as thing from 'thing';
关于javascript - 为什么 TypeScript 将 .default 添加到全局定义的导入中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148057/