Alon
Alon

Reputation: 11885

Declaration file for class-module (TypeScript)

I use a node module that's not found by typings and doesn't exist in definelytyped.

the basic use of the module is:

import * as SomeClass from 'some-module';

var someObject = new SomeClass("some string");
someObject.someMethod();

As you can see, this module exports a class as its default. I haven't figured out how to write the declaration file for it.

This is the best I managed to do:

declare module 'some-module' {
    export default class SomeClass {
        constructor (someArg: string);
        someMethod(): void;
    }
}

BTW it does work JavaScriptly. It's only the TypeScript bothers me.

Any ideas how to solve that?

Upvotes: 2

Views: 1311

Answers (2)

unional
unional

Reputation: 15589

For the declaration, you need to do this:

declare module 'some-module' {
    class SomeClass {
        constructor (someArg: string);
        someMethod(): void;
    }
    namespace SomeClass {}
    export = SomeClass;
}

UPDATE: Thank you to Blake Embrey for pointing out, the hack namespace SomeClass {} is needed to get this working. see https://github.com/Microsoft/TypeScript/issues/5073 for more detail.

Upvotes: 3

OrcusZ
OrcusZ

Reputation: 3660

If you just want to avoid TypeScript error and do not worry about ItelliSense just declare your class library like that in the begining of you file for example:

declare var SomeClass: any;

Upvotes: 0

Related Questions