简体   繁体   English

'req' 已声明,但它的值从不。 参数“req”隐式具有“任何”类型

[英]'req' is declared but its value is never . Parameter 'req' implicitly has an 'any' type

This my code这是我的代码

const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
  res.end('Hello, world.');
});
server.listen(port);

i get an error我收到一个错误

'req' is declared but its value is never. 'req' 已声明,但它的值从不。 Parameter 'req' implicitly has an 'any' type参数“req”隐式具有“任何”类型

I know three ways to fix a mistake.1 // @ts-ignore , 2 (req:any, res:any) , 3 "noImplicitAny": false .我知道修复错误的三种方法。1 // @ts-ignore , 2 (req:any, res:any) , 3 "noImplicitAny": false I do not like all these ways.If a different approach to solve this problem?.我不喜欢所有这些方法。如果用不同的方法来解决这个问题?

Set noUnusedLocals to false in your tsconfig to ignore unused variable errors for your whole code base.noUnusedLocals设置为false以忽略整个代码库中未使用的变量错误。 See compiler options .请参阅编译器选项 Otherwise, use one of the other approaches you mentioned.否则,请使用您提到的其他方法之一。

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html

Parameters declaration with names starting with _ are exempt from the unused parameter checking.名称以 _ 开头的参数声明免于未使用的参数检查。

So to address the first error, use something like:因此,要解决第一个错误,请使用以下内容:

const server = http.createServer((_req, res) => {

or even甚至

const server = http.createServer((_, res) => {

To address the second error, you will either have to explicitly annotate req as any or import the IncomingMessage type from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/http.d.ts .要解决第二个错误,您必须将req显式注释为any或从https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/http.d.ts导入IncomingMessage类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM