I'm wanting to use typescript in my node/express environment, hosting in the Cloud 9 ide.
我想在我的node/express环境中使用typescript,在Cloud 9 ide中托管。
I've got a problem trying to get the compiler to compile app.ts It comes up with several errors of which Property 'bodyParser' does not exist on type 'typeof e' is one of them
我有一个问题试图让编译器编译应用程序,它提出了几个错误,其中一个属性“bodyParser”不存在于类型的“e类型”中。
I have several definition files in the root folder of the application, namely express.d.ts, node.d.ts, body-parser.d.ts. I added body-parser.d.ts in desperation thinking the body parser error would be solved.
我在应用程序的根文件夹中有几个定义文件,即express.d。ts,node.d。ts,body-parser.d.ts。我添加了body-parser.d。在绝望中的ts,认为身体解析器错误将被解决。
The command line is: tsc --sourcemap --module commonjs app.ts The code in app.ts is as follows:
命令行是:tsc——sourcemap——模块commonjs应用程序。ts的代码如下:
// Import express with body parsers (for handling JSON)
import express = require('express');
var bodyParser = require('body-parser');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var expressValidator = require('express-validator');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(expressValidator([]));
// add session support!
app.use(express.cookieParser());
app.use(express.session({ secret: 'sauce' }));
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// Uncommend this line to demo basic auth
// app.use(express.basicAuth((user, password) => user == "user2" && password == "password"));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
function restrict(req, res, next) {
if (req.session.authenticated) {
next();
} else {
res.redirect('/');
}
}
// Get
app.get('/', routes.index);
app.get('/login', routes.login);
app.get('/register', routes.register);
app.get('/users', user.list);
app.get('/AddPlayer', routes.addPlayer);
app.get('/dashboard', restrict, routes.dashboard);
app.get('/logout', routes.logout);
// POST
app.post('/AddPlayer', routes.AddPlayer);
app.post('/login', routes.loginUser);
app.post('/register', routes.registerUser);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
2 个解决方案
#1
1
You didn't add any type information to your ts file the error 'typeof e'
is typeof express which is declared but has no type.
您没有向ts文件中添加任何类型信息,错误的“e类型”是已声明但没有类型的express类型。
express in this case is simply the identifier, the name of the variable.
在本例中,express仅仅是标识符,变量的名称。
The only Typescript feature you used is the import statement but it might not be needed in that case
您所使用的惟一类型文件是import语句,但在这种情况下可能不需要。
I'll go with a var, and forget about TS module definitions. simply var express = require('express')
我将使用一个var,并忘记TS模块的定义。简单的var express = require('express')
Instead of throwing type definitions in your root folder, try using TSD or Nuget if you are on Windows ( I'll strongly recommend TSD over Nuget) .
与其在根文件夹中抛出类型定义,还不如在Windows上使用TSD或Nuget(我将强烈推荐TSD / Nuget)。
This will bring some order to your project and things will start to make sense. tsd makes it very easy for you start with
这将为您的项目带来一些秩序,事情将开始有意义。tsd让你从一开始就很容易。
On command line
在命令行
npm init
npm install tsd --save-dev
#you couldalso install it globally first , with
# npm install tsd -g
# once done, initialize it
tsd init
#then get the definitions you need
tsd query node express --action install --save
tsd will place the definitions under typings\tsd.d.ts for you
then you can add the reference to your script
然后您可以添加对脚本的引用。
/// <reference path='typings/tsd.d.ts' />
now its time to add the type info
现在是添加类型信息的时候了。
It Still won't work but at least it will compile :)
它仍然不能工作,但至少可以编译:)
It the first time have look at the express definition file and I have the feeling it might not help much,. Express has changed bit lately and Im not sure how accurate can the definition file be. and on top of that Express is quite dynamic, the d.ts is full of any's .. :) I'm not sure if you will get any real gain from the type definitions with express but You can still get some nice features out Typescript and build your types/interfaces or extend the existing ones with what you are expecting .
这是第一次看到express definition文件,我觉得它可能没什么帮助。Express最近有点变化,我不确定它的定义文件有多准确。最上面的是动态的d。ts充满了任何东西。我不确定您是否会从express的类型定义中获得真正的收益,但是您仍然可以得到一些漂亮的特性,并构建您的类型/接口,或者使用您所期望的扩展现有的类型。
#2
2
I dont think you should be using TSD , typings is the new standard . And you can get it using npm . For getting express through typings , you can use these commands then to install all dependencies for express
我认为你不应该用TSD,排版是新的标准。你可以使用npm。为了通过类型进行表达,您可以使用这些命令来安装express的所有依赖项。
typings install express --ambient --save
typings install serve-static --ambient --save
typings install express-serve-static-core --ambient --save
typings install mime --ambient --save
#1
1
You didn't add any type information to your ts file the error 'typeof e'
is typeof express which is declared but has no type.
您没有向ts文件中添加任何类型信息,错误的“e类型”是已声明但没有类型的express类型。
express in this case is simply the identifier, the name of the variable.
在本例中,express仅仅是标识符,变量的名称。
The only Typescript feature you used is the import statement but it might not be needed in that case
您所使用的惟一类型文件是import语句,但在这种情况下可能不需要。
I'll go with a var, and forget about TS module definitions. simply var express = require('express')
我将使用一个var,并忘记TS模块的定义。简单的var express = require('express')
Instead of throwing type definitions in your root folder, try using TSD or Nuget if you are on Windows ( I'll strongly recommend TSD over Nuget) .
与其在根文件夹中抛出类型定义,还不如在Windows上使用TSD或Nuget(我将强烈推荐TSD / Nuget)。
This will bring some order to your project and things will start to make sense. tsd makes it very easy for you start with
这将为您的项目带来一些秩序,事情将开始有意义。tsd让你从一开始就很容易。
On command line
在命令行
npm init
npm install tsd --save-dev
#you couldalso install it globally first , with
# npm install tsd -g
# once done, initialize it
tsd init
#then get the definitions you need
tsd query node express --action install --save
tsd will place the definitions under typings\tsd.d.ts for you
then you can add the reference to your script
然后您可以添加对脚本的引用。
/// <reference path='typings/tsd.d.ts' />
now its time to add the type info
现在是添加类型信息的时候了。
It Still won't work but at least it will compile :)
它仍然不能工作,但至少可以编译:)
It the first time have look at the express definition file and I have the feeling it might not help much,. Express has changed bit lately and Im not sure how accurate can the definition file be. and on top of that Express is quite dynamic, the d.ts is full of any's .. :) I'm not sure if you will get any real gain from the type definitions with express but You can still get some nice features out Typescript and build your types/interfaces or extend the existing ones with what you are expecting .
这是第一次看到express definition文件,我觉得它可能没什么帮助。Express最近有点变化,我不确定它的定义文件有多准确。最上面的是动态的d。ts充满了任何东西。我不确定您是否会从express的类型定义中获得真正的收益,但是您仍然可以得到一些漂亮的特性,并构建您的类型/接口,或者使用您所期望的扩展现有的类型。
#2
2
I dont think you should be using TSD , typings is the new standard . And you can get it using npm . For getting express through typings , you can use these commands then to install all dependencies for express
我认为你不应该用TSD,排版是新的标准。你可以使用npm。为了通过类型进行表达,您可以使用这些命令来安装express的所有依赖项。
typings install express --ambient --save
typings install serve-static --ambient --save
typings install express-serve-static-core --ambient --save
typings install mime --ambient --save