我有一个外部JS库全局参数:如何在定义文件中定义全局TypeScript变量以便导入?
function Thing() { ... }
...
var thing = new Thing();
有一个打字稿定义文件,所以在thing.d.ts
:
declare var thing: ThingStatic;
export default thing;
export interface ThingStatic {
functionOnThing(): ThingFoo;
}
export interface ThingFoo {
... and so on
然后我导入我自己的TS文件这跟:
import thing from 'thing';
import {ThingFoo} from 'thing';
...
const x:ThingFoo = thing.functionOnThing();
的问题是,transpiles到:
const thing_1 = require("thing");
...
thing_1.default.functionOnThing();
这会引发错误。我问了一下,在another question和建议是使用:
import * as thing from 'thing';
这不能解决问题 - 它给了我在TS thing.default
但随后这是不确定的,一旦transpiled到JS。
我认为thing.d.ts
有什么问题 - 必须有一种方法来定义可导入的类型化全局参数。
我应该如何编写thing.d.ts
以便正确表示JS,并且不会包含default
或其他实际不存在的属性?
尝试使用字符串名称声明 - '''声明var'thing'。我并不真正理解_Why_这个作品,但我必须在稍微不同的情况下做同样的事情。(在我的情况下,我从声明模块foo切换到声明模块'foo') – user1821052