Property '' does not exist on type 'Request<ParamsDictionary>'
Asked Answered
K

6

18

When trying to extend the Request interface from the package express to add some custom properties, I'm getting the following typescript error:

TS2339: Property '' does not exist on type 'Request<ParamsDictionary>'.

Do you know how to solve that?

Kirchner answered 20/11, 2019 at 14:54 Comment(0)
K
29

Since a recent update of its typings and dependencies, I found that the following should fix the errors in your application.

In your tsconfig.json

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

And in your custom typings

// custom_typings/express/index.d.ts
declare namespace Express {
    interface Request {
        customProperties: string[];
    }
}
Kirchner answered 20/11, 2019 at 14:54 Comment(4)
yeah, it does fix but when just created index.d.ts file on root folder my VScode stopped showing error but I was still getting error on terminal, so I did as you described but directly created index.d.ts file inside custom_typings and still it was giving error, it went after creating express folder, so why is that express folder mandatory ?Deranged
You can either create a directory named express or having a file named express.d.ts to tell typescript that you are aiming this specific moduleKirchner
@RohitAmbre did you figure out how to fix it. Vscode seems to have no issues but compiler showing some errors.Silversmith
@danie_man, I did like what I have explained in my comment and it works without any compiler issue you can check hereDeranged
P
5

I recently had the same issue, I followed the solution in the previous comments and this repo and I still had the same issue. After doing more digging it seems like it's a bug with ts-node.

To solve this you need to run your server with a --files flag

So if you normally run your server ts-node ./src/server.ts or nodemon ./src/server.ts Change it to ts-node --files ./src/server.ts or nodemon --files ./src/server.ts

After that, I was able to get rid of both the VScode errors and errors while starting the server.

Persaud answered 8/1, 2022 at 0:40 Comment(1)
Thank you for your answer you save my day ! I didn't understand why vscode told me my config is right but by running the project it still shows the errors. I didn't expected to be a ts-node bug...Parmenter
S
4

Just add the following, what this does is it simply adds a custom property to the express Request interface

declare global {
  namespace Express {
    interface Request {
      propertyName: string; //or can be anythin
    }
  }
}
Subedit answered 29/8, 2020 at 21:53 Comment(1)
Thank you for this, could you explain how this works a bit more? I've never seen someone use declare globalTuner
D
0

In my case it was missing types for express. What I'm currently working on is migrating our codebase from Yarn to PNPM. The difference with PNPM is it doesn't hoist dependencies the way Yarn does so I had to add the dependencies on the package.json for each workspace that would use that dependency.

This is the error I encountered:

TSError: ⨯ Unable to compile TypeScript:
../server/src/api/filters/googleFilter.ts:6:23 - error TS2339: Property 'headers' does not exist on type 'Request<core.ParamsDictionary>'.

6   const idToken = req.headers.authorization;

It took me quite a few searches to look for a fix when I decided to open up the node_modules folder of that workspace. Inside node_modules/@types/express/index.d.ts

/// <reference types="express-serve-static-core" />
/// <reference types="serve-static" />

import * as bodyParser from "body-parser";
import serveStatic = require("serve-static");
import * as core from "express-serve-static-core";
import * as qs from "qs";

I saw my IDE showing errors telling me that it cannot find the types for express-serve-static-core and serve-static so what I did was to add it on the package.json of that workspace and that fixed the errors on the terminal.

Hope this helps someone else who will encounter the same issue with PNPM.

Disendow answered 6/12, 2022 at 10:6 Comment(0)
J
0

This worked for me!

Using ts-node

Add the following file to add a property to the express Request interface as suggested by @Rishav Sinha

  1. Add this file src/types/types.custom.d.ts
declare global {
    declare namespace Express {
        interface Request {
            user?: any,
            page?: number,
        }
    }
}

// If this file has no import/export statements (i.e. is a script)
// convert it into a module by adding an empty export statement.
export { }
  1. Add in tsconfig.json
{
  "compilerOptions": {
    //...
    "typeRoots": [
      "./types",
      "./node_modules/@types"
    ],
  }
// ...
}
  1. Run this command with --files options as suggested by @Shahar Sharron

If you installed globally ts-node

$ ts-node --files ./src/index.ts

or to run from your project dependencies ts-node

$ npx ts-node --files ./src/index.ts

Using nodemon

If you want to use nodemom

  1. Add this file in folder project nodemon.json
{
    "watch": ["src/**/*.ts"],
    "ext": "ts,json",
    "ignore": [
        "src/**/*.spec.ts",
        "src/**/*.test.ts"
    ],
    "exec": "npx ts-node --files ./src/index.ts"
}
  1. Run nodemon
$ nodemon
Joseph answered 17/1, 2023 at 8:49 Comment(0)
H
0

Only these two changes solved errors for me both vscode and compilation:

First create your declaration file and put this code a folder named typings:

Folder name is important:

typings/ or src/typings/

File name is not important:

index.d.ts global.d.ts index.ts ...

import { Request, Response } from "express";

declare global {
  namespace Express {
    export interface Request {
      propertyNameA: string;
      propertyNameB: string;
    }
    export interface Response {
      propertyNameA: string;
      propertyNameB: string;
      propertyNameC: string;
    }
  }
}

// This line has no function
export { Request, Response };

Now, there are two alternatives:

A. Add --files flag to your ts-node or nodemon execution expression

Example: ts-node --files src/app.ts

B. Or update ts-config.json file like:

{
  "ts-node": { 
    "files": true 
  },
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    /* --- */
  }
}
Hage answered 28/2, 2024 at 22:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.