Ambient declations for javascript modules that add fields to interfaces in other modules

191 views Asked by At

Suppose I want to use something like express. So I add

/// <reference path=".../express.d.ts" />
import express = require("express");

and can then use types like ExpressServerRequest.

So far so good. But now I want to use, for example, the connect-flash javascript module. This adds an additional flash function to ExpressServerRequest. I'm struggling to see how to define connect-flash.d.ts so that req.flash() is defined on instances of ExpressServerRequest if I've imported the connect-flash module, and not defined otherwise. Can this behaviour be captued in TypeScript?

1

There are 1 answers

2
Fenton On

If the item you are extending is an interface, they are open in TypeScript, so you can just add the extras like so:

interface ExpressServerRequest {
    myAdditionalThing(): void;
}

There are ways of declaring pretty much anything, so if this isn't the trick for your case just post the exiting ExpressServerRequest and I'll update the example. The version of express.d.ts I checked on Definitely Typed has ExpressServerRequest as an interface.