oguzh4n oguzh4n - 18 days ago 6
TypeScript Question

Typescript compiler throw error: 'error TS2304: Cannot find name' in class

I want to use external javascript library (MyLib) without typings in my XClient.ts file so I created a dump declaration file for external lib, (myLib.d.ts) When I import myLib to XClient, everything is ok in XClient constructor but typescript compiler throw error at "readonly _lib: MyLib;" line of XClient.ts

Compiler error: " error TS2304: Cannot find name 'MyLib'

How can I use external library without typings properly? And why compiler throw error?

// 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;
}

Answer

Cannot find name 'MyLib'

Its there as a variable but not as a type. More on this : https://basarat.gitbooks.io/typescript/content/docs/project/declarationspaces.html

Fix

Probably:

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