打字稿编译器抛出错误:在类

时间:2016-11-15 13:29:43

标签: typescript typescript2.0

我想在我的XClient.ts文件中使用外部javascript库(MyLib)而没有打字,所以我为外部lib创建了一个转储声明文件,(myLib.d.ts)当我将myLib导入XClient时,一切正常XClient构造函数但是typescript编译器在XClient.ts的“ readonly _lib:MyLib; ”行中抛出错误

编译器错误:“错误TS2304:找不到名称'MyLib'

如何正确使用没有打字的外部库?为什么编译器会抛出错误?

// XClient.ts

import * as $ from "jquery";
import * as MyLib  from "myLib";

export class XClient {
    readonly _modelId: string;
    readonly _lib: MyLib;

    constructor(modelId: string) {
        this._modelId = modelId;

        this._lib = new MyLib(this._modelId);
    }
}

// myLib.d.ts

declare var inner: any;
declare module "myLib" {
    export = inner;
}

1 个答案:

答案 0 :(得分:0)

  

找不到姓名' MyLib'

它作为变量而不是类型。更多相关信息:https://basarat.gitbooks.io/typescript/content/docs/project/declarationspaces.html

修复

可能:

declare module "myLib" {
    var inner: any;
    type inner = any;
    export = inner;
}