【发布时间】:2017-04-30 05:29:23
【问题描述】:
我有一个带全局参数的外部 JS 库:
function Thing() { ... }
...
var thing = new Thing();
有一个TypeScript定义文件,所以在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();
问题是转译为:
const thing_1 = require("thing");
...
thing_1.default.functionOnThing();
这会引发错误。我在another question 中询问过这个问题,建议使用:
import * as thing from 'thing';
这并不能解决问题 - 它在 TS 中给了我thing.default,但是一旦转译为 JS,它就没有定义了。
我认为thing.d.ts 有问题 - 必须有一种方法来定义可以导入的类型化全局参数。
我应该如何编写 thing.d.ts 以便它正确地表示 JS 并且不会转换为包含 default 或其他实际不存在的属性?
【问题讨论】:
-
尝试用字符串名称声明 - '''declare var 'thing'。我真的不明白 为什么 这行得通,但我必须在稍微不同的情况下做同样的事情。 (在我的情况下,我从声明模块 foo 切换到声明模块 'foo')
标签: javascript typescript typescript-typings