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

type check failed when register the plugin #216

Closed
2 tasks done
ianzone opened this issue Mar 11, 2023 · 10 comments
Closed
2 tasks done

type check failed when register the plugin #216

ianzone opened this issue Mar 11, 2023 · 10 comments

Comments

@ianzone
Copy link

ianzone commented Mar 11, 2023

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

Plugin version

10.1.0

Node.js version

v18.12.0

Operating system

Linux

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

Ubuntu 22.04.2

Description

Argument of type 'FastifyHelmet' is not assignable to parameter of type 'FastifyPluginCallback<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.
  Type 'FastifyHelmet' is not assignable to type 'FastifyPluginCallback<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger>'.
    Types of parameters 'instance' and 'instance' are incompatible.
      Type 'import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/instance").FastifyInstance<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, import("http").IncomingMessage, import("http").ServerRes...' is not assignable to type 'import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/instance").FastifyInstance<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, import("http").IncomingMessage, import("http").ServerRes...'.
        The types returned by 'withTypeProvider().after()' are incompatible between these types.
          Type 'import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/instance").FastifyInstance<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, import("http").IncomingMessage, import("http").ServerRes...' is not assignable to type 'import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/instance").FastifyInstance<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, import("http").IncomingMessage, import("http").ServerRes...'.
            Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, Provider> & PromiseLike<...>' is not assignable to type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, Provider>'.
              Types of property 'decorateReply' are incompatible.
                Type '<T>(property: string | symbol, value: T extends (...args: any[]) => any ? (this: import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/reply").FastifyReply<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils")...' is not assignable to type '<T>(property: string | symbol, value: T extends (...args: any[]) => any ? (this: import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/reply").FastifyReply<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils")...'.
                  Types of parameters 'value' and 'value' are incompatible.
                    Type 'T extends (...args: any[]) => any ? (this: import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/reply").FastifyReply<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, ... 6 more ..., unk...' is not assignable to type 'T extends (...args: any[]) => any ? (this: import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/reply").FastifyReply<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, ... 6 more ..., unk...'.
                      Type 'T | ((this: FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, ... 4 more ..., unknown>, ...args: Parameters<T>) => ReturnType<...>)' is not assignable to type 'T extends (...args: any[]) => any ? (this: FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, ... 4 more ..., unknown>, ...args: Parameters<T>) => ReturnType<...> : T'.
                        Type 'T' is not assignable to type 'T extends (...args: any[]) => any ? (this: FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, ... 4 more ..., unknown>, ...args: Parameters<T>) => ReturnType<...> : T'.
                          Type '(this: import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/reply").FastifyReply<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, ... 6 more ..., unknown>, ...args: Parameters<T>) => Re...' is not assignable to type '(this: import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/reply").FastifyReply<import("/home/ian/templates/lambda-nestify/node_modules/.pnpm/[email protected]/node_modules/fastify/types/utils").RawServerDefault, ... 6 more ..., unknown>, ...args: Parameters<T>) => Re...'.
                            The 'this' types of each signature are incompatible.
                              Type 'FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, RouteGenericInterface, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>' is missing the following properties from type 'FastifyReply<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, RouteGenericInterface, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>': cspNonce, helmetts(2345)
instance.d.ts(124, 17): This type parameter might need an `extends T extends (...args: any[]) => any ? (this: FastifyReply<RawServerDefault, http.IncomingMessage, http.ServerResponse<http.IncomingMessage>, ... 4 more ..., unknown>, ...args: Parameters<T>) => ReturnType<...> : T` constraint.

Steps to Reproduce

https://docs.nestjs.com/security/helmet#use-with-fastify

Expected Behavior

No response

@Uzlopak
Copy link
Contributor

Uzlopak commented Mar 11, 2023

Duplicate of #210

I also provided a PR in upstream helmet project. So it is a helmet issue.

@ianzone
Copy link
Author

ianzone commented Apr 29, 2023

@Uzlopak would you pls update the dependency since the upstream is updated?

@paynecodes
Copy link

I've tried overriding @fastify/helmet depedency on helmet by forcing [email protected]. This issue remains.

@jhiode
Copy link

jhiode commented Jun 27, 2023

I don't understand why but TypeScript (v4.9.5) is happy when I change the FastifyHelmetOptions type to this:

--- a/node_modules/@fastify/helmet/types/index.d.ts
+++ b/node_modules/@fastify/helmet/types/index.d.ts
@@ -27,9 +27,9 @@ declare namespace fastifyHelmet {
     helmet?: Omit<FastifyHelmetOptions, 'global'> | false;
   }
 
-  export interface FastifyHelmetOptions extends NonNullable<HelmetOptions> {
+  export type FastifyHelmetOptions = NonNullable<HelmetOptions> & {
     enableCSPNonces?: boolean,
     global?: boolean;
   }
 
   export const fastifyHelmet: FastifyHelmet

I couldn't reprouce this behaviour with the TS Playground though:
https://www.typescriptlang.org/play?ts=4.9.5#code/C4TwDgpgBAEhA2BbCwDyZgEsD2A7AzlALxQDeAUAJABm22A-AFxS4CuiARhAE5QA+UDnXgQAhrnIBfcuUy5gPaqIDG0AGKj8WaiDhIU6LHkIQAHgtwATQgDk8N1vHiiOIgDx7kaDDgIA+MnIoYMFRbiYWdi5uABogkI5RAC8Itk4eAG4pGWVjYChsH2NmDS1MHU8DIoJiQJCoWmxmAEY4+sTuZgAmOOlyXIJsEQA6eGwAcwAKQqMCAEosoA

Maybe that helps someone.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 27, 2023

We actually fixed this upstream in helmet?! Do you still experience this issue with latest version of fastify-helmet?

@jhiode
Copy link

jhiode commented Jun 28, 2023

Yes, we still have that problem. And we are on the latest version of fastify-helmet but not on the latest version of helmet though as that is limited by fastify-helmet:

"helmet": "^6.0.0"

Btw, do you have a link to the fix for me?

@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 28, 2023

@fastify/helmet is actually supporting all version 6. And if you make a npm i it should load the latest helmet, which is v6.2.0. It got fixed in v6.1.0.

See
helmetjs/helmet#405

@jhiode
Copy link

jhiode commented Jun 28, 2023

Yes, we are on v6.2.0 and the error is also different.

  await fastify.register(helmet, {
    contentSecurityPolicy: false,
    crossOriginEmbedderPolicy: false,
    crossOriginOpenerPolicy: false,
    crossOriginResourcePolicy: false,
  });
error TS2769: No overload matches this call.
  Overload 1 of 3, '(plugin: FastifyPluginCallback<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '{ contentSecurityPolicy: boolean; crossOriginEmbedderPolicy: boolean; crossOriginOpenerPolicy: boolean; crossOriginResourcePolicy: boolean; }' is not assignable to parameter of type 'FastifyRegisterOptions<FastifyHelmetOptions> | undefined'.
      Object literal may only specify known properties, and 'contentSecurityPolicy' does not exist in type 'FastifyRegisterOptions<FastifyHelmetOptions>'.
  Overload 2 of 3, '(plugin: FastifyPluginAsync<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '{ contentSecurityPolicy: boolean; crossOriginEmbedderPolicy: boolean; crossOriginOpenerPolicy: boolean; crossOriginResourcePolicy: boolean; }' is not assignable to parameter of type 'FastifyRegisterOptions<FastifyHelmetOptions> | undefined'.
      Object literal may only specify known properties, and 'contentSecurityPolicy' does not exist in type 'FastifyRegisterOptions<FastifyHelmetOptions>'.
  Overload 3 of 3, '(plugin: FastifyPluginAsync<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger> | FastifyPluginCallback<...> | Promise<...> | Promise<...>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '{ contentSecurityPolicy: boolean; crossOriginEmbedderPolicy: boolean; crossOriginOpenerPolicy: boolean; crossOriginResourcePolicy: boolean; }' is not assignable to parameter of type 'FastifyRegisterOptions<FastifyHelmetOptions> | undefined'.
      Object literal may only specify known properties, and 'contentSecurityPolicy' does not exist in type 'FastifyRegisterOptions<FastifyHelmetOptions>'.

73     contentSecurityPolicy: false,

But I could create a new issue for this.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 28, 2023

Hmm. Maybe, because of

contentSecurityPolicy: typeof contentSecurityPolicy;

Maybe should be:

type FastifyHelmet = FastifyPluginAsync<fastifyHelmet.FastifyHelmetOptions & {
  contentSecurityPolicy: typeof contentSecurityPolicy;
}>;

@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 28, 2023

Please create a new issue and a repo where you provide a minimal reproducable example of the issue you experience.

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