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

Usage of RouteShorthandOptions breaks type safety #186

Open
2 tasks done
leosuncin opened this issue Nov 5, 2024 · 0 comments
Open
2 tasks done

Usage of RouteShorthandOptions breaks type safety #186

leosuncin opened this issue Nov 5, 2024 · 0 comments

Comments

@leosuncin
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.0.0

Plugin version

5.0.0

Node.js version

22.10.0

Operating system

Linux

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

CachyOS 2024.10.03

Description

When using RouteShorthandOptions to declare the variable for the options of a route handler, it breaks its type safety, this doesn't happen if the variables isn't explicit typed.

import Fastify from 'fastify';
import { fastifyAutoload } from '@fastify/autoload';
import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox';

const app = Fastify()
  .withTypeProvider<TypeBoxTypeProvider>()
  .register(fastifyAutoload, {
    dir: `${import.meta.dirname}/routes`,
    routeParams: true,
  });

app.listen({ port: 8080 });
import {
  FastifyPluginAsyncTypebox,
  Type,
} from '@fastify/type-provider-typebox';
import { RouteShorthandOptions } from 'fastify';

const options: RouteShorthandOptions = {
  schema: {
    querystring: Type.Object({ name: Type.String() }),
    response: {
      200: Type.Object({ greeting: Type.String() }),
    },
  },
};

const route: FastifyPluginAsyncTypebox = async (fastify) => {
  fastify.get('/hello', options, async (request) => {
    return { greeting: `hello ${request.query.name}` };
                                     // ^? request.query is of type unknown
  });
};

export default route;

Link to code that reproduces the bug

No response

Expected Behavior

I hope to get the same type safety as omitting the explicit type declaration

import {
  FastifyPluginAsyncTypebox,
  Type,
} from '@fastify/type-provider-typebox';

const options = {
  schema: {
    querystring: Type.Object({ name: Type.String() }),
    response: {
      200: Type.Object({ greeting: Type.String() }),
    },
  },
};

const route: FastifyPluginAsyncTypebox = async (fastify) => {
  fastify.get('/hello', options, async (request) => {
    return { greeting: `hello ${request.query.name}` };
                                     // ^? request.query is of { name: string }
  });
};

export default route;
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

1 participant