Using Express () with TypeScript

I want to use the latest version of Expess with node.js in TypeScript. Express.d.ts provided by microsoft in the samples seems to be built on versions prior to 3.0.x. In the 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 found to hack this: I added the following line to Express.d.ts:

 export function(): any; 

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

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

This seems to fix the problem, it compiles without errors, 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 problem?

+4
source share
4 answers

A pretty old discussion, but I recently ran into the same problem and found that the DefiniteTyped site has a new express.d.ts which properly supports Express 3.

+8
source

You must be able to add this environmental function declaration to express.d.ts in order to get what you want.

declare function express(): ExpressServer;

+2
source

if you declare express like this: import * as express from "express" , you will get this error at runtime by declaring it like this: const express = require "express" will not throw any error.

Also, be sure to declare the app variable or property type as express.Application

0
source

Here's an example project - an Express 4.x application in TypeScript: https://github.com/czechboy0/Express-4x-Typescript-Sample

-3
source

Source: https://habr.com/ru/post/1437596/


All Articles