【问题标题】:fastify route request and response handler type in Typescript在 Typescript 中 fastify 路由请求和响应处理程序类型
【发布时间】:2020-12-24 10:45:27
【问题描述】:

有人知道 fastify 中的请求和响应处理程序类型是什么吗?

现在我只是使用'any',typescript eslint 给了我一个警告:

fastify.post('/ac', async(req: any , res: any) => {

【问题讨论】:

  • 你用的是什么版本?
  • 我使用的是版本 3。
  • @Alvin 你能用 tslint 代替 eslint 试试吗

标签: typescript fastify


【解决方案1】:

您正在寻找的适当类型分别是“FastifyRequest”和“FastifyReply”。

它们可以如下所示导入和实现。

import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';

fastify.post('/ac', async (req: FastifyRequest, res: FastifyReply) => {

});

【讨论】:

  • 当我尝试访问 req.body.object 时,提示 err at object - Property 'object' does not exist on type 'unknown'.ts(2339)
  • fastify.io/docs/latest/TypeScript/#request 有助于(type CustomRequest = FastifyRequest<{ Body: .. }> 部分)输入请求正文
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 2011-02-23
  • 2020-06-19
  • 2012-02-10
相关资源
最近更新 更多