How To Type - dojo/typings GitHub Wiki

To type Dojo 1 type classes, there are two different interfaces that need to be typed.

Let's assume the source code uses dojo/_base/declare and the following is specified in a module named dojo/MyEventedClass:

define([
    'dojo/_base/declare',
    'dojo/Evented',
    'dojo/_base/lang'
], function (declare, Evented, lang) {
    var MyEventedClass = declare([Evented], {
        constructor: function (opts) {
            lang.mixin(this, opts);
        },
        foo: 'foo',
        bar: function () { return 'bar'; }
    });

    MyEventedClass.static = function () { return 'static' });

    return MyEventedClass;
});

You would want to create the following typings:

declare namespace dojo {
    interface MyEventedClass extends Evented {
        foo: string;
        bar(): string;
    }

    interface MyEventedClassConstructor {
        new (opts?: Object): MyEventedClass;
        prototype: MyEventedClass;
    }
}

declare module 'dojo/MyEventedClass' {
    const MyEventedClass: dojo.MyEventedClassConstructor;
    export = MyEventedClass;
}

By convention, the first interface represents the instance when used with the new keyword on the constructor and is named the classname. The second interface is the constructor interface, which would include an unamed new method which behaves like the class constructor, returning an instance. The name of this interface is [ClassName]Constructor. This would also include all the static methods/properties on the class constructor function.

When declaring the module, using the full MID and exporting a const with the name of the instance, but referencing the constructor interface.

And then to use the module in TypeScript, it would look something like this:

import * as MyEventedClass from 'dojo/MyEventedClass';

const myEvented = new MyEvetnedClass();
MyEventedClass.static();
myEvented.foo = 'bar';
myEvented.bar();