2

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?

1
  • Are you transpiling your code down to es5? If you do, then I would recommend you to use import SomeClass = require('some-module') instead....it's a long story short Commented Mar 26, 2017 at 3:25

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It produces an error: 'TS2497: Module '"some-module"' resolves to a non-module entity and cannot be imported using this construct.' on the import statement. I transpile to ES6 BTW.
You need to put it in a separate file. Not in a file with top-level import/export. Save it as custom-typings/some-module.d.ts and include it in your tsconfig.json.
Is it an application or a package?
shoot, seems like something changed in 2.2 and this does not work anymore. Need to figure out what's the alternative myself.
It's in a separate file. I've created some declaration files already. I know how to do that. It's just a special case because it has a default export which is a class.
|
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.