-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.19.2
Plugin version
No response
Node.js version
20.x
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.4.1
Description
I'm trying to customize my 404 experience, but the handler return type for setNotFoundHandler is complaining. Everything works as expected when I @ts-ignore the line so I think this is just a bug in the type system. I have a similar handler for my index / route and it does not have the typescript error.
The complete TypeScript error is:
Argument of type '(request: FastifyRequest<RouteGenericInterface, Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ... 4 more ..., ResolveFastifyRequestType<...>>, reply: FastifyReply<...>) => FastifyReply<...> | ... 1 more ... | { ...; }' is not assignable to parameter of type '(request: FastifyRequest<RouteGenericInterface, Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ... 4 more ..., ResolveFastifyRequestType<...>>, reply: FastifyReply<...>) => void | Promise<...>'.
Type 'FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, RouteGenericInterface, unknown, FastifySchema, FastifyTypeProviderDefault, unknown> | "Not found\n" | { ...; }' is not assignable to type 'void | Promise<unknown>'.
Type 'FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, RouteGenericInterface, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>' is not assignable to type 'void | Promise<unknown>'.
Type 'FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, RouteGenericInterface, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>' is missing the following properties from type 'Promise<unknown>': catch, finally, [Symbol.toStringTag]ts(2345)
Do any current type maintainers have an idea what the fix is?
Steps to Reproduce
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import fastify from 'fastify';
const app = fastify({
logger: true,
});
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
void app.register(import('@fastify/accepts'));
void app.register(import('@fastify/static'), {
root: join(__dirname, '../public'),
});
// TypeScript fails here at `handler`
app.setNotFoundHandler(function handler(request, reply) {
const accept = request.accepts();
switch (accept.type(['html', 'json'])) {
case 'html': {
void reply.type('text/html');
return reply.sendFile('404.html');
}
case 'json': {
void reply.type('application/json');
return {"message":`Route ${request.method}:${request.url} not found`,"error":"Not Found","statusCode":404}
}
default: {
void reply.code(404);
return 'Not found\n';
}
}
})
// But TypeScript does not fail here
app.get('/', function handler(request, reply) {
const accept = request.accepts();
switch (accept.type(['html', 'json'])) {
case 'html': {
void reply.type('text/html');
return reply.sendFile('index.html');
}
case 'json': {
void reply.type('application/json');
return { data: `Welcome to my app!` };
}
default:
return `Welcome to my app!\n`;
}
});
export { app };Expected Behavior
I expect TypeScript not to fail.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels