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

Conditionals in FastifyRequest based on ContextConfig broken since 4.19.0 #5322

Open
2 tasks done
BrianValente opened this issue Feb 19, 2024 · 1 comment · May be fixed by #5355
Open
2 tasks done

Conditionals in FastifyRequest based on ContextConfig broken since 4.19.0 #5322

BrianValente opened this issue Feb 19, 2024 · 1 comment · May be fixed by #5355

Comments

@BrianValente
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

4.26.1

Plugin version

No response

Node.js version

18.17.1

Operating system

macOS

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

14.3.1

Description

Hello! In my project I have conditionals for the FastifyRequest type based on the route config in FastifyContextConfig. For example, I can add a needsUserCountry config to FastifyContextConfig like this:

declare module 'fastify' {
  interface FastifyRequest<ContextConfig extends FastifyContextConfig> {
    country: ContextConfig['needsUserCountry'] extends true ? string : never;
  }

  interface FastifyContextConfig {
    needsUserCountry?: boolean;
  }
}

And then create a route like this:

app.get(
  '/countryStringLength',
  {
    config: {
      needsUserCountry: true,
    },
  },
  async (req) => req.country.length,
);

The config will be handled by a hook, so the value will be set and the type is correct.

If I set needsUserCountry to false or I don't set it, it will throw an error due to req.country being never:
image

And if I set it to true, it will work.
image

In newer releases this behavior is broken.

In 4.19.0 and 4.19.1 the config type requires url and method, passing random values makes it work. The behavior of needsUserCountry works as expected.
image
image
image

Since 4.20 url and method are no longer required, but now configs are not being detected, as if I didn't pass a config object.
image

Steps to Reproduce

Try this code with Fastify 4.18 and 4.26.1. In 4.18 it will work, in 4.26.1 types will fail.

import fastify from 'fastify';

const app = fastify();

declare module 'fastify' {
  interface FastifyRequest<ContextConfig extends FastifyContextConfig> {
    country: ContextConfig['needsUserCountry'] extends true ? string : never;
  }

  interface FastifyContextConfig {
    needsUserCountry?: boolean;
  }
}

app.get(
  '/countryStringLength',
  {
    config: {
      needsUserCountry: true,
    },
  },
  async (req) => req.country.length,
);

Expected Behavior

I expect the req object passed to the handler to have the correct types based on the config object of the endpoint.

@BrianValente BrianValente linked a pull request Mar 11, 2024 that will close this issue
4 tasks
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

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

Successfully merging a pull request may close this issue.

2 participants