Typescript编译错误:类型'typeof e'不存在属性'bodyParser' [英] Typescript compile error: Property 'bodyParser' does not exist on type 'typeof e'

查看:153
本文介绍了Typescript编译错误:类型'typeof e'不存在属性'bodyParser'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Cloud 9 ide中托管的节点/表达环境中使用打字稿.

I'm wanting to use typescript in my node/express environment, hosting in the Cloud 9 ide.

我在尝试让编译器编译app.ts时遇到问题.其中出现了一些错误,其中类型'typeof e'上的Property'bodyParser'不存在

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

我在应用程序的根文件夹中有几个定义文件,分别是express.d.ts,node.d.ts,body-parser.d.ts.我绝望地添加了body-parser.d.ts,认为可以解决body解析器错误.

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.

命令行是:tsc --sourcemap --module commonjs app.tsapp.ts中的代码如下:

The command line is: tsc --sourcemap --module commonjs app.ts The code in app.ts is as follows:

// 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'));
});

推荐答案

您没有在ts文件中添加任何类型信息错误'typeof e'是typeof express,已声明但没有类型.

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.

在这种情况下,express只是标识符,即变量的名称.

express in this case is simply the identifier, the name of the variable.

您使用的唯一Typescript功能是import语句,但在这种情况下可能不需要使用

The only Typescript feature you used is the import statement but it might not be needed in that case

我将使用var,然后忘记TS模块的定义.只需 var express = require('express')

I'll go with a var, and forget about TS module definitions. simply var express = require('express')

如果您使用的是Windows,请尝试使用 TSD 或Nuget,而不是在根文件夹中添加类型定义.我会强烈推荐在Nuget上使用TSD).

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) .

这将为您的项目带来一些秩序,事情将开始变得有意义.tsd使您非常容易以

This will bring some order to your project and things will start to make sense. tsd makes it very easy for you start with

在命令行上

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 :)

这是第一次查看快速定义文件,我觉得它可能没有多大帮助.Express最近发生了一些变化,我不确定定义文件的准确性如何.最重要的是Express是非常动态的,d.ts充满了任何.. :)我不确定您是否会从使用express的类型定义中获得真正的收获,但是您仍然可以从Typescript中获得一些不错的功能,并构建您的类型/接口或以期望的方式扩展现有的类型/接口.

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 .

这篇关于Typescript编译错误:类型'typeof e'不存在属性'bodyParser'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆