Skip to content

Type error in setupFastifyErrorHandler #18619

@OliverRM

Description

@OliverRM

Environment

SaaS (https://sentry.io/)

Steps to Reproduce

  1. Set up Fastify and TypeScript
  2. Enable TypeScript feature exactOptionalPropertyTypes
  3. Use Sentry.setupFastifyErrorHandler

Expected Result

No type errors

Actual Result

The following type error occurs:

error TS2345: Argument of type 'FastifyInstance<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault> & PromiseLike<...> & { ...; }' is not assignable to parameter of type 'FastifyInstance'.
  The types of 'register(...).addHook' are incompatible between these types.
    Type '{ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = unknown, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger, Fn extends onRequestAsyncHookHandler<...> | onRequestHookHandler<...> = onRequestHookHandler<...>>(name: "onReque...' is not assignable to type '{ (hook: string, handler: HandlerOriginal): FastifyInstance; (hook: "onError", handler: (request: FastifyRequest, reply: FastifyReply, error: Error) => void): FastifyInstance; (hook: "onRequest", handler: (request: FastifyRequest, reply: FastifyReply) => void): FastifyInstance; }'.
      Types of parameters 'hook' and 'handler' are incompatible.
        Type 'HandlerOriginal' is not assignable to type 'onRequestAsyncHookHandler<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>, ... 4 more ..., any> | onRequestHookHandler<...>'.
          Type '(request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => Promise<void>' is not assignable to type 'onRequestAsyncHookHandler<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>, ... 4 more ..., any> | onRequestHookHandler<...>'.
            Type '(request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => Promise<void>' is not assignable to type 'onRequestAsyncHookHandler<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>, ... 4 more ..., any>'.
              Target signature provides too few arguments. Expected 3 or more, but got 2.

See https://github.com/OliverRM/sentry-fastify-type-error for a minimal failing example.

Product Area

Unknown

Link

No response

DSN

No response

Version

No response

Metadata

Metadata

Assignees

No fields configured for issues without a type.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions