문제

I am trying to make a express app with typescript.

This is my code so far:

//<reference path="./server/types/node.d.ts"/>
//<reference path="./server/types/express.d.ts"/>

import express = require('express');

var app = express();

app.get('/', function(req, res) {
    res.send('hi');
});

app.listen(3000);

nothing really shocking, I am just trying to make this work, but somehow, always when I try translate this file to a js file. I get strange errors, even if I change the express version to 3.1 (the express.d.ts is just supported for express 3.1 not for 4.x)

Any idea, where I can get a express.d.ts file for express 4.x or what I am doing wrong?

>> error TS2071: Unable to resolve external module ''express''
>> error TS2071: Module cannot be aliased to a non-module type.
>> error TS2095: Could not find symbol 'express'.
도움이 되었습니까?

해결책

You reference comments are wrong. There needs to be three slashes /// :

///<reference path="./server/types/node.d.ts"/>
///<reference path="./server/types/express.d.ts"/>

The only way you can get that error if you are using this reference file https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts#L26 was that your reference comments were wrong and typescript was not reading that express.d.ts :)

다른 팁

Might I add that because you are using express 4.x and the type definitions are not up-to-date, you will be missing some key features that are central to express 4.x, such as Routers.

You have two choices at this point: update the type definitions, or use

var express = require('express');

instead, which removes some of the benefits that typescript provides, but is something you're bound to run into in the future with other node modules, such as mongoose.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top