TypeScript: How to define custom typings for installed npm package?

Create a new file, called it rx-node.d.ts and make sure it's referenced in your tsconfig.json either directly or from another file, for example typings/index.d.ts. Then you can start with something like this:

///<reference path="rx.d.ts" />

declare namespace RxNode  {

    export interface Emitter<T>{

    }

    export function toEventEmitter<T>(observable: Rx.Observable<T>, eventName: string): Emitter<T>;
}

This question/answer might be helpuful: How to write d.ts file from existing .js file?

If you create a nice, high quality typings definition file, contribute to https://github.com/DefinitelyTyped/DefinitelyTyped


Beware, this is old question (2016) regarding managing definitions using typings which is deprecated in favor of installing them straight via npm


You can add custom definitions to typings.json. For example having following folder structure:

/typings
    /custom
        rx-node.d.ts
    /global
        ...

where rx-node.d.ts is your custom typings file. For example:

declare module RxNode {
    export interface ...
}

Then install with a command

typings install file:typings/custom/rx-node.d.ts --save --global

Or manually: in typings.json add reference to this typings file:

{
    "name": "TestName",
    "version": false,
    "globalDependencies": {
        "rx-node": "file:typings/custom/rx-node.d.ts"
    }
}

And run typings install