Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a TypeScript declaration file for an external commonjs module that has constructor?

PLEASE SEE MORE DETAILED QUESTION: How do I write a TypeScript declaration file for a complex external commonjs module that has constructor, such as imap?

I write TypeScript for a Node.js app, and I want to write a TypeScript declaration file for a javascript module (available from npm) that has a constructor at the module level.

Here is a simplified version of the relevant code, in file a.js:

function A(config) {
    this.state = 'constructed';
}
A.prototype.update = function() {
    this.state = 'updated';
};
module.exports = A;

and a simplified javascript application, app.js, that uses module a:

var mod = require('a');
var i = new mod({});
console.log('i.state=' + i.state);
i.update();
console.log('i.state=' + i.state);

How do I write a TypeScript declaration file for module a.js?

I've read the TypeScript Guide for Writing Definition (.d.ts) Files but unfortunately, I couldn't figure out how to apply the guidelines to this case.


updated to include interfaces

Here is my declaration file a.d.ts:

declare module 'a' {
    import events                           = require('events');
    import EventEmitter                     = events.EventEmitter;

    interface Config {
        foo: number;
    }
    interface Other {
        baz: number;
    }

    class A extends EventEmitter {
        state: string;
        constructor(config: Config);
        update(): void;
    }

    var out: typeof A;
    export = out;
}

I can't figure out how to make the interfaces available to my TypeScript app. I also want to keep them within the module, so that names like Config don't collide with those from other modules.


Added TypeScript app that uses this declaration

This is what I expect my app.ts to look like:

import mod = require('a');
import Config = mod.Config;
import Other = mod.Other;

var other : Other = {a: 2};
var config : Config = {foo: 2};
var i = new mod(config);
console.log('i.state=' + i.state)
i.update();
console.log('i.state=' + i.state)
like image 692
psnider Avatar asked Sep 30 '22 16:09

psnider


1 Answers

There are a couple ways to do this, here is one:

declare class A {
    state: string;
    constructor(config: any);
    update(): void;
}

declare module 'a' {
    var out: typeof A;

    export = out;
}

EDIT: If you want to include interfaces, but also have an exported class, you can set it up like this:

declare module A {
    class A {
        state: string;
        constructor();
        update(): void;
    }

    interface B {
        value: any;
    }
}

declare module 'a' {
    var out: typeof A.A;

    export = out;
}
like image 174
wjohnsto Avatar answered Oct 03 '22 02:10

wjohnsto