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

using @fastify/helmet together with an extended FastifyRequest type leads to a type-error #223

Closed
2 tasks done
jhiode opened this issue Jun 28, 2023 · 5 comments
Closed
2 tasks done

Comments

@jhiode
Copy link

jhiode commented Jun 28, 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.18.0

Plugin version

10.1.0

Node.js version

v18.15.0

Operating system

macOS

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

13.4.1

Description

By using @fastify/helmet together with extending the FastifyRequest type with custom attributes (as described here) we get the following TS error:

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,

Relates to #216 (comment)

Steps to Reproduce

It should be enough to use the TypeScript (v4.9.5) with the following files:

index.ts:

import helmet from '@fastify/helmet';
import setupFastify from 'fastify';

const fastify = await setupFastify({ ... });

await fastify.register(helmet, {
  contentSecurityPolicy: false,
  crossOriginEmbedderPolicy: false,
  crossOriginOpenerPolicy: false,
  crossOriginResourcePolicy: false,
});

tsconfig.json:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "rootDir": "./",
    "target": "ES2019",
  },
}

fastify.d.ts:

import 'fastify';

declare module 'fastify' {
  interface FastifyRequest {
    startTime: number;
  }
}

Expected Behavior

No response

@jhiode
Copy link
Author

jhiode commented Jun 28, 2023

Well, it seems to also fail without the FastifyRequest thingy.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 28, 2023

Can you please provide a repository to clone with everything set up?

@jhiode
Copy link
Author

jhiode commented Jun 28, 2023

@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 28, 2023

Duplicate of #219

Please use latest helmet 10.1.1, which covers the issue.

@Uzlopak Uzlopak closed this as completed Jun 28, 2023
@Uzlopak
Copy link
Contributor

Uzlopak commented Jun 28, 2023

@jhiode
Because of your remark regarding using helmet v6, we released today a new major version of fastify-helmet using latest helmet ;).

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

2 participants