0

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?

basarat
  • 261,912
  • 58
  • 460
  • 511

1 Answers1

1

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.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Thanks Steve. However, what I wanted was to extend the interface in the connect-flash.d.ts file. If I do this, and reference both express.d.ts and connect-flash.d.ts I was hoping that ExpressServerRequest would have been extended in the calling code. However, this doesn't seem to happen so I have to extend the interface again in the caller. So how would you define a myAdditionalThing.d.ts file to extend ExpressServerRequest so that if the caller referenced both d.ts files they could use the property, but it wouldn't be visible if they didn't reference myAdditionalThing.d.ts? – user2711129 Aug 25 '13 at 10:49
  • Hi @Steve, any thoughts on whether what I'm trying to do is currently possible in TypeScript. – user2711129 Aug 30 '13 at 19:18