Edg*_*gar 2 node.js typescript
这是我的代码
const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
res.end('Hello, world.');
});
server.listen(port);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
'req' 已声明,但其值永远不会。参数“req”隐式具有“any”类型
我知道三种纠正错误的方法// @ts-ignore。1、2、3 。我不喜欢所有这些方法。是否有不同的方法来解决这个问题?(req:any, res:any)"noImplicitAny": false
小智 12
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html
名称以 _ 开头的参数声明免于未使用的参数检查。
因此,要解决第一个错误,请使用以下内容:
const server = http.createServer((_req, res) => {
Run Code Online (Sandbox Code Playgroud)
甚至
const server = http.createServer((_, res) => {
Run Code Online (Sandbox Code Playgroud)
要解决第二个错误,您必须显式注释req为或从https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/http.d.tsany导入类型。IncomingMessage