7

I want to use the most recent version of Expess with node.js in TypeScript. The express.d.ts provided by microsoft in the samples seems to be built upon a versions prior to 3.0.x. In previous version you could do

var app = express.createServer()

but after 3.0.x you should do:

var app = express();

Express.d.ts does not support this... I've found a hack around this: I've added the following line to Express.d.ts:

export function(): any;

In app.ts when I want to create the app object I do the following:

var app = <express.ExpressServer>express();

This seems to fix the issue, it's compiling without an error, and also I get intellisense support. However this is a hack... First of all why can't I write something like this?

export function(): ExpressServer;

Is this the recommended way to fix this issue?

4 Answers 4

9

Pretty old discussion, but I ran into the same problem recently and found that there is a new express.d.ts that properly supports express 3 on the DefinitelyTyped site.

Sign up to request clarification or add additional context in comments.

Comments

2

You should be able to add this ambient function declaration to express.d.ts to get what you want.

declare function express(): ExpressServer;

3 Comments

Not sure what you mean.... i want this this: ///<reference path='express.d.ts'/> import express = module('express'); import vlcApi = module('vlc-node'); var app = <express.ExpressServer>express(); and i have this in the express.d.ts file: ///<reference path='node.d.ts' /> declare module "express" { export function createServer(): ExpressServer; export function static(path: string): any; export function(): any; //this is my hack since export function(): ExpressServer; does not work import http = module("http"); export var listen; Could you clarify please?
Ah. I see now they have put the ExpressServer interface inside the module express declaration. That's why it isn't accessible from outside as in my example. Looks like that express.d.ts file would have to be refactored to move those interface declarations into the outer-scope so that the ambient function declaration I showed can be added.
I get it now. Thats a definitely a way to go. :)
0

if you declare express this way: import * as express from "express", you will get this error in runtime, declaring it this way: const express = require "express", won't throw any error.

Also, don't forget to declare app variable or property type as express.Application

Comments

-4

Here's a sample project - Express 4.x app in TypeScript: https://github.com/czechboy0/Express-4x-Typescript-Sample

6 Comments

that sample project includes absolutely 0 actual typescript syntax. The only thing I actually see that has to do with typescript is your .tsd file
That's not true, see the many .ts files there, which are written in TypeScript. But the point of the project isn't to teach TypeScript anyway, just to show a working Express app with TypeScript, because it can be tedious to actually migrate a js project to a ts project under Express. But don't use it if you don't like it, nobody's forcing you :)
I see the .ts files, but they are just written in pure javascript. Hence my comment "that sample project includes absolutely 0 typescript syntax" I couldn't care less if there are .ts files in there if all that they contain is pure vanilla javascript code. You can't claim that is an example of Express with Typescript by simply renaming the files to a .ts extension from .js and adding a .tsd file, that isn't how it works.
I stand corrected. I will mention there are are multiple places were javascript syntax is used rather than typescript.
To bad the sample uses any as the type for Express and thereby not solving the problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.