Skip to content

FastifyInterface TypeScript augmentation when using namespaces #321

Open

Description

@chiptoma

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @fastify/jwt@7.2.4 for the project I'm working on.

When using the namespace option, the fastify.jwt object no longer contains the JWT interface, but the namespace objects.

In my case, I created two namespaces auth and refresh and the fastify.jwt now contains auth: JWT, refresh: JWT.

Unfortunately, I wasn't able to override the augmentation, so I commented it out for the moment.

Maybe this should be left for the implementation to be typed.

Here is the diff that solved my problem:

diff --git a/node_modules/@fastify/jwt/types/jwt.d.ts b/node_modules/@fastify/jwt/types/jwt.d.ts
index d9bc2b4..7d1605b 100644
--- a/node_modules/@fastify/jwt/types/jwt.d.ts
+++ b/node_modules/@fastify/jwt/types/jwt.d.ts
@@ -13,9 +13,11 @@ import {
 } from 'fastify'
 
 declare module 'fastify' {
-  interface FastifyInstance {
-    jwt: fastifyJwt.JWT
-  }
+  // PATCH: If namespaces are used, the `jwt` object will contain the namespaces as a property.
+  // This augmentation prevents later augmentations from overwriting the namespace property.
+  // interface FastifyInstance {
+  //   jwt: fastifyJwt.JWT
+  // }
 
   interface FastifyReply {
     jwtSign(payload: fastifyJwt.SignPayloadType, options?: fastifyJwt.FastifyJwtSignOptions): Promise<string>

This issue body was partially generated by patch-package.

Activity

  1. mcollina commented on Dec 24, 2023

    @mcollina
    Member

    This is definitely something we would need to fix for Fastify v5 (fastify/fastify#5061)

  2. piotr-cz commented on Jan 15, 2024

    @piotr-cz

    Here is my workaround when using the 'player' namespace.

    I found that FastifyJwtNamespace doesn't augment FastifyRequest and FastifyReply interfaces, but incorrectly FastifyInstance.

    import type { FastifyPluginCallback } from 'fastify'
    import type { FastifyJWTOptions, FastifyJwtNamespace } from '@fastify/jwt'
    import jwt from '@fastify/jwt'
    
    declare module 'fastify' {
      interface FastifyInstance extends FastifyJwtNamespace<{ namespace: 'player' }> {}
    
      interface FastifyRequest {
        // Custom namespace
        playerJwtVerify: FastifyRequest['jwtVerify'],
        playerJwtDecode: FastifyRequest['jwtDecode'],
      }
    
      interface FastifyReply {
        // Custon namespace
        playerJwtSign: FastifyReply['jwtSign'],
      }
    }
    
    declare module '@fastify/jwt' {
      // Custom Namespace (undocumented)
      interface JWT { player: jwt.JWT }
    }
    
    const jwtPlayerPlugin: FastifyPluginCallback = async (fastify) => {
      fastify.register(jwt, {
        namespace: 'player',
      })
    })
  3. hobi9 commented on Feb 8, 2024

    @hobi9

    I'm having the same issue, it looks like the types are wrong

  4. added
    typescript
    on Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions