Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with Fastify version 4.* in TypeScript: 'register' method expects 0 arguments, but got 1 in 'fastifyRoutes' #5272

Open
2 tasks done
tanmoy-witbybit opened this issue Jan 15, 2024 · 5 comments

Comments

@tanmoy-witbybit
Copy link

tanmoy-witbybit commented Jan 15, 2024

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.25.0

Plugin version

No response

Node.js version

20.8.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Ubuntu 22.04.3 LTS

Description

Index.ts file

import app from './app';

const PORT = Number(process.env.PORT) || 8080;
const ENV = process.env.ENV || 'development';
app()
	.then(app => {
		app.listen({ port: PORT }, (err, address) => {
			if (err) {
				console.error(err);
				process.exit(1);
			}
			console.log('Server running at port -> %d in %s mode', PORT, ENV);
		});
	})
	.catch(err => {
		console.log(err);
	});

app.ts

import Fastify from 'fastify';
import routes from './routes';

export default async function app() {
	const fastify = Fastify({
		logger: true
	});
	await fastify.register(routes, { prefix: '/' });
	return fastify;
}

routes.ts

import { FastifyInstance } from 'fastify';
export type FRequest = FastifyRequest<{
	Params: { [key: string]: string };
	Body: { [key: string]: any };
	Querystring: { [key: string]: string };
}>;
export default async function fastifyRoutes(fastify: FastifyInstance) {
	fastify.route({
		method: 'POST',
		url: '/user/me',
		preHandler: [
			async (request: FRequest, reply) => {
				await foo(request, reply);
			},
			async (request: FRequest, reply) => {
				await foo2({},request,reply);
			}
		],
		handler: async function (request: FRequest, reply) {
			await foo3({}, request, reply);
		}
	});
}

I am encountering an issue when using Fastify version 4.* with TypeScript. The error message suggests a problem with the 'register' method in the 'fastifyRoutes' function within my codebase. Specifically, it indicates that the 'routes'' method expects 0 arguments but receives 1. Same thing happens with other methods like GET, POST ...

i user ts-node --files src/index.ts to run the code.

image

But when i try to user Fastify version 3.* it works perfectly no issue.
Also if i try to run ts-node **-T** --files src/index.ts my code works but i don't want to do so.

Steps to Reproduce

  1. Use Fastify version 4.* in a TypeScript project.
  2. Attempt to run the server using the command: ts-node --files src/index.ts
  3. Observe the error related to the 'register' method in the 'fastifyRoutes' function.

Expected Behavior

No response

@mcollina
Copy link
Member

There is an option object passed to your plugin as an option.

@tanmoy-witbybit
Copy link
Author

tanmoy-witbybit commented Jan 15, 2024

Can you please share a code sample, that could be very helpful. like how to use fastify.route() properly. @mcollina

@mcollina
Copy link
Member

Can you please upload all your files in a repository?

Note that there is no call to register() in the example, and in the last message you message .route().

@tanmoy-witbybit
Copy link
Author

Thanks for the response, as you can see in app.ts file I have use the register to use the routes. The issue is that when i passed the route in the app.register() and then it throws the error (a type error cause without type checking the code works fine).

@bowber
Copy link

bowber commented Apr 17, 2024

I had the same issue, upgrading to Typescript 5 should fixed this.
After I sneak into the source code, I find out that it uses const modifier to a type parameter, this is a new feature of Typescript 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@mcollina @bowber @tanmoy-witbybit and others