frouriojs / frourio

Fast and type-safe full stack framework, for TypeScript

Home Page:https://frourio.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

callParserIfExistsQuery で型エラー

technote-space opened this issue · comments

Description

ERROR in /Users/***/server/$server.ts(135,42)
      TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>'.

fastify の最新のリリースでコンテキストのチェックが追加されたようです。

fastify/fastify#3143
https://github.com/fastify/fastify/releases/tag/v3.18.1

 export interface preValidationHookHandler<
   RawServer extends RawServerBase = RawServerDefault,
   RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
   RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
   RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
   ContextConfig = ContextConfigDefault
 > {
   (
+    this: FastifyInstance<RawServer, RawRequest, RawReply>,
     request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
     reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
     done: HookHandlerDoneFunction
   ): void;
 }

とりあえず

const callParserIfExistsQuery = (parser: preValidationHookHandler): preValidationHookHandler => (req, reply, done) =>
  Object.keys(req.query as any).length ? parser(req, reply, done) : done()

const callParserIfExistsQuery = (parser: preValidationHookHandler): preValidationHookHandler => (req, reply, done) =>
 Object.keys(req.query as any).length ? parser : done()

に書き換えると型エラーは消えました。

エラー箇所は以下のように呼ばれていたので

preValidation: callParserIfExistsQuery(parseNumberTypeQueryParams([['take', true, false], ['skip', true, false]])),

parseNumberTypeQueryParamsconsole.log を仕込んだ状態でAPIを呼び出して上記の変更後も parseNumberTypeQueryParams が呼ばれることは一応確認できましたが正しい修正の仕方かはわかりません。

Environment

  • Package version: v0.25.0
  • OS:
    • Linux
    • Windows
    • macOS
  • Node.js version: v15.10.0
  • npm version: 7.5.3

Additional context

お待たせしました
@IshinoJun が修正してくれました
v0.25.1 にアップデートしてみてください