How to add a property in FastifyRequest's interface without overwriting everything in a declaration file

12 Views Asked by At

How do I add a property to a interface using declarations, without overwriting everything in this object.

declare module 'fastify' {
  interface FastifyRequest {
      user: User;
  }
}


//auth.ts

...
    const user = jwt.verify(
        req.headers.authorization ?? '', // Property 'headers' does not exist on type 'FastifyRequest'.ts(2339)
...
1

There are 1 best solutions below

0
Gabriel Nadaleti On

I Found a solution:

Simply add a import with the module your declaring or add a triple slash reference like:

/// <reference path="<path-to-typings-dir>" />
or
import fastify from "fastify";

declare module 'fastify' {
    export interface FastifyRequest {
        user: any;
    }
}