Search code examples
node.jsexpresstypescriptamd

Extending external module with d.ts in typescript


I am building a nodejs application using typescript. I'm using several external libraries like express.js. As many, this library as well designed to be extendable.

I'd like to extend it by adding a custom method. What is the typescript best practice to do so?

I wanted to inherit a class from it, but its d.ts doesn't define any classes (obviously). I can extend the interface:

declare module Express {
    export interface Application {
        foo();
    }
}

but I cannot figure out how to actually implement it.

Any help would be appreciated.


Solution

  • You shouldn't add members to Application directly unless you want to make a reusable library that other people should use. If that is indeed the case you will need function interception (http://basarat.github.io/this-and-prototype/#/apply).

    I recommend just making a utility function.