Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – Typescript 2.2 Express req,res隐式任意大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在为节点/快递项目添加类型方面有点蠢蠢欲动.

我正在使用TypeScript 2.2并表达4.x并且我通过npm安装了类型:

npm install –save-dev @ types / express

import * as express from "express"

const app: express.Application = express()

app.get("/Hello",(req,res) => {
  res.send("world")
})

这给了我:

src/app.ts(33,22): error TS7006: Parameter 'req' implicitly has an 'any' type.
src/app.ts(33,27): error TS7006: Parameter 'res' implicitly has an 'any' type.

我试图避免为所有请求处理程序执行此操作:

(req: express.request,res: express.ResponsE) => {}

在我看来它应该能够推断出那些.我错了吗?这不可能吗?

tsconfig.json:

{
  "compilerOptions": {
    "target": "es6","module": "commonjs","noImplicitAny": true,"sourceMap": true,"outDir": "dist","typeRoots": ["src/types","node_modules/@types"]
  },"include": [
    "src/**/*.ts"
  ]
}

谢谢!

解决方法

快速库的get方法过载(请参阅此处获取演示 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/14cfa9f41c2835fcd22e7243a32b25253c310dee/express-serve-static-core/index.d.ts#L25-L40)

interface requestHandler {
    (req: request,res: Response,next: NextFunction): any;
}

interface ErrorrequestHandler {
    (err: any,req: request,next: NextFunction): any;
}

type PathParams = String | RegExp | (String | RegExp)[];

type requestHandlerParams = requestHandler | ErrorrequestHandler | (requestHandler | ErrorrequestHandler)[];

interface IRouterMatcher<T> {
    (path: PathParams,...handlers: requestHandler[]): T;
    (path: PathParams,...handlers: requestHandlerParams[]): T;
}

requestHandlerParams使得无法可靠地确定req和res有什么.建议:暂时注释一下

大佬总结

以上是大佬教程为你收集整理的node.js – Typescript 2.2 Express req,res隐式任意全部内容,希望文章能够帮你解决node.js – Typescript 2.2 Express req,res隐式任意所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。