fastify / aws-lambda-fastify

Insipired by aws-serverless-express to work with Fastify with inject functionality.
MIT License
498 stars 32 forks source link

App FastifyInstance type error when using ZodTypeProvider #205

Closed rt-joe closed 3 months ago

rt-joe commented 5 months ago

Prerequisites

Fastify version

4.26.0

Plugin version

4.0.0

Node.js version

20.x

Operating system

macOS

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

Sonoma 14.4

Description

When using Fastify type provider ZodTypeProvider from https://github.com/turkerdev/fastify-type-provider-zod we get the following type error when passing our app to awsLambdaFastify():

No overload matches this call.
  Overload 1 of 2, '(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, options?: LambdaFastifyOptions | undefined): PromiseHandler<...>', gave the following error.
    Argument of type 'FastifyZodInstance' is not assignable to parameter of type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.
      The types returned by 'after()' are incompatible between these types.
        Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, ZodTypeProvider> & PromiseLike<...>' is not assignable to type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault> & PromiseLike<...>'.
          Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, ZodTypeProvider> & PromiseLike<...>' is not assignable to type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.
            The types of 'addSchema(...).addHook' are incompatible between these types.
              Type '{ <RouteGeneric extends import("<redacted>/node_modules/fastify/types/route").RouteGenericInterface = import("<redacted>/node_modules/fastify/types/route").RouteGenericInterface, ContextConfig = unknown, SchemaCompiler extends import("<redacted>...' is not assignable to type '{ <RouteGeneric extends import("<redacted>/node_modules/fastify/types/route").RouteGenericInterface = import("<redacted>/node_modules/fastify/types/route").RouteGenericInterface, ContextConfig = unknown, SchemaCompiler extends import("<redacted>...'. Two different types with this name exist, but they are unrelated.
                Types of parameters 'hook' and 'hook' are incompatible.
                  Types of parameters 'opts' and 'opts' are incompatible.
                    Type 'RouteOptions<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, any, any, any, ZodTypeProvider, FastifyBaseLogger> & { ...; }' is not assignable to type 'RouteOptions<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, any, any, any, FastifyTypeProviderDefault, FastifyBaseLogger> & { ...; }'.
                      Type 'RouteOptions<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, any, any, any, ZodTypeProvider, FastifyBaseLogger> & { ...; }' is not assignable to type 'RouteOptions<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, any, any, any, FastifyTypeProviderDefault, FastifyBaseLogger>'.
                        Types of property 'handler' are incompatible.
                          Type 'RouteHandlerMethod<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, any, any, any, ZodTypeProvider, FastifyBaseLogger>' is not assignable to type 'RouteHandlerMethod<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, any, any, any, FastifyTypeProviderDefault, FastifyBaseLogger>'.
                            Type 'FastifyTypeProviderDefault' is not assignable to type 'ZodTypeProvider'.
  Overload 2 of 2, '(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, options?: LambdaFastifyOptions | undefined): CallbackHandler<...>', gave the following error.
    Argument of type 'FastifyZodInstance' is not assignable to parameter of type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.ts(2769)

Which boils down to Type 'FastifyTypeProviderDefault' is not assignable to type 'ZodTypeProvider'. I'm not sure if that is the actual error or a red herring.

Our Fastify instance type is defined as:

import type {
  FastifyBaseLogger,
  FastifyInstance,
  RawReplyDefaultExpression,
  RawRequestDefaultExpression,
  RawServerDefault,
} from "fastify";
import type { ZodTypeProvider } from "fastify-type-provider-zod";

export type FastifyZodInstance = FastifyInstance<
  RawServerDefault,
  RawRequestDefaultExpression<RawServerDefault>,
  RawReplyDefaultExpression<RawServerDefault>,
  FastifyBaseLogger,
  ZodTypeProvider
>;

I'm not sure if we're doing something wrong or fastify-type-provider-zod or fastify-aws-lambda. I can post the full non-truncated type error if that helps. Any help is appreciated!

Steps to Reproduce

Reproducible repo https://github.com/rt-joe/fastify-aws-lambda-zod-type-provider-type-error

With node v20 installed

git clone https://github.com/rt-joe/fastify-aws-lambda-zod-type-provider-type-error
cd fastify-aws-lambda-zod-type-provider-type-error
npm install
npm run type

Expected Behavior

No type error when using non default fastify type provider

mcollina commented 5 months ago

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

rt-joe commented 5 months ago

I would love to but I haven't been able to figure out the root issue. Our current workaround is to cast it to FastifyInstance

export const handler = fastifyLambda(app as unknown as FastifyInstance);

Not a major issue or blocker by any means.

driimus commented 3 months ago

More of an issue with the type provider itself, rather than aws-lambda-fastify.

Assignability issues got fixed a while ago for the official type providers (fastify/fastify#4342 for context) - the zod type provider slipped through as it's a third party package that was only recently brought into the docs.

@rt-joe I've submitted a PR for the type provider, but it might take a while for you to reap the benefits considering that package hasn't been updated since January 2023. Since it's a very small change, you could consider patching the dependency (e.g. https://pnpm.io/cli/patch or https://www.npmjs.com/package/patch-package) with the changes from the linked PR.

mcollina commented 3 months ago

If the package has not been updated, we could think of bringing it in the org if there is a need.

driimus commented 3 months ago

@rt-joe fix has been published, so you should be good to go once you upgrade to fastify-type-provider-zod@1.2.0.

rt-joe commented 3 months ago

Worked like a charm! Much appreciated 🙏