Zoltan Arvai
Zoltan Arvai

Reputation: 71

Using Express() with TypeScript

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?

Upvotes: 7

Views: 16927

Answers (4)

MajidJafari
MajidJafari

Reputation: 1110

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

Upvotes: 0

Wade Hatler
Wade Hatler

Reputation: 1835

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.

Upvotes: 9

czechboy
czechboy

Reputation: 944

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

Upvotes: -4

joshuapoehls
joshuapoehls

Reputation: 35217

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

declare function express(): ExpressServer;

Upvotes: 2

Related Questions