Skip to content

Cannot pass in RawServer generic type #191

@olyop

Description

@olyop

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

Plugin version

4.1.0

Node.js version

18.7.0

Operating system

Linux

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

Fedora Workstation 36

Description

The default export fp only allows a Options generic. This means you cannot pass in a function of type FastifyPluginAsync<MyOptions, MyRawServer, MyTypeProvider> for instance. I believe you should be able to pass in the two other generics as it has some use cases, in my case creating a package that can accept any RawServer type.

This results in a ts error like this:

Argument of type 'FastifyPluginAsync<Options, RawServer, FastifyTypeProviderDefault>' is not assignable to parameter of type 'FastifyPluginCallback<Options, Server, FastifyTypeProviderDefault>'.
  Types of parameters 'instance' and 'instance' are incompatible.
    ...

This is the types I believe would fix this, happy to make a pull request.

export default function fp<
  Options extends FastifyPluginOptions = Record<never, never>,
  Server extends RawServerBase = RawServerDefault,
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
>(
  fn: FastifyPluginAsync<Options, Server, TypeProvider>,
  ...
): FastifyPluginAsync<Options, Server, TypeProvider>

Steps to Reproduce

Pass in a function of type FastifyPluginAsync<MyOptions, MyRawServer, MyTypeProvider> to the fp function.

Expected Behavior

You should be able to pass in the generics manually or be inferred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions