Skip to content

Commit 4942b07

Browse files
authored
fix(node): relax Fastify's setupFastifyErrorHandler argument type (#18620)
When using [exactOptionalPropertyTypes](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes), it is pretty hard to match Fastify's instance types given all the missing overloads from the partial signature we have. The problematic area is around `addHook`. We either need to have all the signatures implemented exactly, or outright import Fastify's types which is not ideal since it is not a dependency. I opted to relax the types specifically for `setupFastifyErrorHandler` by providing a minimal instance with the one method it needs to avoid TS trying to matching the other properties and methods signatures, not ideal but simple enough. I verified that this works for Fastify v3 throughout v5 closes #18619
1 parent 8787a87 commit 4942b07

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/node/src/integrations/tracing/fastify/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { generateInstrumentOnce } from '@sentry/node-core';
1515
import { DEBUG_BUILD } from '../../../debug-build';
1616
import { FastifyOtelInstrumentation } from './fastify-otel/index';
17-
import type { FastifyInstance, FastifyReply, FastifyRequest } from './types';
17+
import type { FastifyInstance, FastifyMinimal, FastifyReply, FastifyRequest } from './types';
1818
import { FastifyInstrumentationV3 } from './v3/instrumentation';
1919

2020
/**
@@ -244,7 +244,7 @@ function defaultShouldHandleError(_error: Error, _request: FastifyRequest, reply
244244
* app.listen({ port: 3000 });
245245
* ```
246246
*/
247-
export function setupFastifyErrorHandler(fastify: FastifyInstance, options?: Partial<FastifyHandlerOptions>): void {
247+
export function setupFastifyErrorHandler(fastify: FastifyMinimal, options?: Partial<FastifyHandlerOptions>): void {
248248
if (options?.shouldHandleError) {
249249
getFastifyIntegration()?.setShouldHandleError(options.shouldHandleError);
250250
}

packages/node/src/integrations/tracing/fastify/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export interface FastifyInstance {
2727
addHook(hook: 'onRequest', handler: (request: FastifyRequest, reply: FastifyReply) => void): FastifyInstance;
2828
}
2929

30+
/**
31+
* Minimal type for `setupFastifyErrorHandler` parameter.
32+
* Uses structural typing without overloads to avoid exactOptionalPropertyTypes issues.
33+
* https://github.com/getsentry/sentry-javascript/issues/18619
34+
*/
35+
export type FastifyMinimal = {
36+
register: (plugin: (instance: any, opts: any, done: () => void) => void) => unknown;
37+
};
38+
3039
export interface FastifyReply {
3140
send: () => FastifyReply;
3241
statusCode: number;

0 commit comments

Comments
 (0)