typescript 明确指定 Express 的“应用程序、请求、响应...”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27676884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:57:08  来源:igfitidea点击:

Explicitly Specifying types for Express' "application, request, response..."

expresstypescript

提问by Andrew Connell

I'm working to update a simple NodeJS + ExpressJS app to use TypeScript. I've got it working, but I'm having fits adding some extra data annotations to add extra type checking and code-time autocomplete/IntelliSense in Visual Studio & WebStorm.

我正在努力更新一个简单的 NodeJS + ExpressJS 应用程序以使用 TypeScript。我已经让它工作了,但我有合适的添加一些额外的数据注释,以在 Visual Studio 和 WebStorm 中添加额外的类型检查和代码时自动完成/智能感知。

I've got the latest express.d.tsreferenced. I've also created the Express app:

我引用了最新的express.d.ts。我还创建了 Express 应用程序:

var app = express();

Everything works... but I can't seem to figure out how to add the type info for appfor autocomplete assistance when you type something like

一切正常...但我似乎无法弄清楚如何为应用程序添加类型信息以在您键入类似内容时提供自动完成帮助

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

I've tried annotating app(and req& res) using Express.Application(and .Request& .Response) but that isn't resolving. Looking at the type definition I'm getting confused with how the typedef is written creating the internal "e" reference...

我试过使用(and & )注释app(和req& res) Express.Application,但这并没有解决。查看类型定义,我对如何编写 typedef 创建内部“e”引用感到困惑....Request.Response

Surprised I can't find anyone asking & resolving this since the express.d.ts typedef was updated for ExpressJS 4.0 (previously I found references to ExpressServerbut that isn't present today.

很惊讶我找不到任何人询问和解决这个问题,因为 express.d.ts typedef 已针对 ExpressJS 4.0 更新(之前我发现了对ExpressServer 的引用,但今天不存在。

Ideas?

想法?

回答by Andrew Connell

Classic case of "work on something for a while with no success, but when you finally post a question asking for help, you figure it out immediately".

经典案例“在某件事上工作了一段时间没有成功,但是当您最终发布问题寻求帮助时,您立即弄清楚了”。

The solution: When you want to use one of the types, you have to import the module as the typedef is in a named module within the typedef.

解决方案:当您想使用其中一种类型时,您必须导入模块,因为 typedef 位于 typedef 内的命名模块中。

In the above code (that resided in my app.ts), I was getting the type annotations on app. Because my import statement at the top of the file was import express = require('express');, I could annotate the req& resparams like this:

在上面的代码(驻留在我的app.ts 中)中,我在app上获得了类型注释。因为我在文件顶部的 import 语句是import express = require('express');,所以我可以像这样注释req& res参数:

app.get('/', function(req:express.Request, res:express.Response){});

In other files, where I was trying to get type annotations on the app, I was missing the import statement at the top of the file. Once added, I could add the annotations for that as well like:

在其他文件中,我试图在app上获取类型注释,但我缺少文件顶部的 import 语句。添加后,我还可以为此添加注释:

public init(app: express.Application){}

回答by Sam

In case anyone stumbles on this question in 2017 and using a more modern TS. You should be able to just npm install @types/expressand then it should just work.

万一有人在 2017 年偶然发现这个问题并使用更现代的 TS。你应该能够npm install @types/express,然后它应该可以正常工作。