title: Unable to extend Express Request in TypeScript
body: <p>This question has been asked several times, but none of the answers that were given have worked for me. I'm trying to extend the Express <code>Request</code> object to include a property to store a <code>User</code> object. I created a declaration file, <code>express.d.ts</code>, and placed it in the same directory as my <code>tsconfig.json</code>:</p>
  
  <pre><code>import { User } from "./src/models/user";
  
  declare namespace Express {
      export interface Request {
          user: User;
      }
  }
  </code></pre>
  
  <p>Then I try to make an assignment to it in <code>secured-api.ts</code>:</p>
  
  <pre><code>import express from 'express';
  import { userService } from '../services/user';
  
  router.use(async (req, res, next) =&gt; {
      try {
          const user = await userService.findByUsername(payload.username);
  
          // do stuff to user...
  
          req.user = user;
          next();
      } catch(err) {
          // handle error
      }
  });
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>src/routes/secured-api.ts:38:21 - error TS2339: Property 'user' does not exist on type 'Request'.
  
  38                 req.user = user;
                         ~~~~
  </code></pre>
  
  <p>My <code>User</code> class is:</p>
  
  <pre><code>import { Model, RelationMappings } from 'objection';
  
  export class User extends Model {
  
      public static tableName = 'User';
      public static idColumn = 'username';
  
      public static jsonSchema = {
          type: 'object',
          required: ['fname', 'lname', 'username', 'email', 'password'],
  
          properties: {
              fname: { type: 'string', minLength: 1, maxLength: 30 },
              lname: { type: 'string', minLength: 1, maxLength: 30 },
              username: { type: 'string', minLength: 1, maxLength: 20 },
              email: { type: 'string', minLength: 1, maxLength: 320 },
              password: { type: 'string', minLength: 1, maxLength: 128 },
          }
      };
  
      public static modelPaths = [__dirname];
  
      public static relationMappings: RelationMappings = {
  
      };
  
      public fname!: string;
      public lname!: string;
      public username!: string;
      public email!: string;
      public password!: string;
  }
  </code></pre>
  
  <p>My <code>tsconfig.json</code> is:</p>
  
  <pre><code>{
      "compilerOptions": {
        "target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
        "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
        "lib": ["es2015"],                             /* Specify library files to be included in the compilation. */
        "outDir": "./build",                      /* Redirect output structure to the directory. */
        "strict": true,                           /* Enable all strict type-checking options. */
        "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      }
  }
  </code></pre>
  
  <p>My directory structure is:</p>
  
  <pre><code>backend/
      package.json
      tsconfig.json
      express.d.ts
      src/
          models/
              user.ts
          routes/
              secured-api.ts
  </code></pre>
  
  <p>What am I doing wrong here?</p>