Visitar URL original
An error TS2339: Property 'user' does not exist on type 'Request...' · Issue #72786 · DefinitelyTyped/DefinitelyTyped · GitHub
Skip to content

An error TS2339: Property 'user' does not exist on type 'Request...' #72786

Open

Description

@alsimona

Hi,

I'm experiencing an issue in my typescript project when trying to extend the request type. While the squiggly lines are gone and hovering over req.user there is "(property) Express.Request.user?: string | undefined," (so no errors in the file and it seems req.user type is recognized), in the terminal I get "an error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

export const verifyToken = (
  req: Request,
  res: Response,
  next: NextFunction
): void | Promise<void> => {
[...]
jwt.verify(token, ACCESS_TOKEN_SECRET, (err, decoded) => {
    if (err || typeof decoded !== "object" || !("username" in decoded)) {
      console.log("token not verified");
      return res.sendStatus(403);
    }
    **req.user = decoded?.username;**
    next();
  }
}

The index.d.ts file is in the src/types folder, so in tsconfig.json file, I've specified:

"compilerOptions": {
"typeRoots": [
      "./src/types",
      "./node_modules/@types"
    ] 
},
"include": ["src", "src/types"]

In the index.d.ts file I've declared:

import { Request } from "express";

declare global {
  namespace Express {
    interface Request {
      user?: string;
    }
  }
}

Why is this happening, how can I fix this?

Activity

  1. Aylos9er commented on Dec 15, 2025

    @Aylos9er

    The user does exist…

  2. mahmoodhamdi commented on Mar 15, 2026

    @mahmoodhamdi

    This is a TypeScript configuration issue rather than a bug in @types/express.

    The problem is likely that your index.d.ts file contains a top-level import statement, turning it into a module. Try removing the import and using the Express namespace directly:

    declare namespace Express {
      interface Request {
        user?: string;
      }
    }

    Or keep your current file but ensure it is properly included via "include" in your tsconfig.json rather than relying on typeRoots.

    This is a usage question best suited for Stack Overflow or the TypeScript Community Discord.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions