Definitelytyped: [@types/express] Latest types throw Property 'headers', 'body', 'query' does not exist on type 'Request<any>'

Created on 4 Nov 2019  ·  30Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

If you know how to fix the issue, make a pull request instead.

  • [X] I tried using the @types/express package and had problems.
  • [X] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [X] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [X] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @borisyankov, @cmuh, @rockwyc992, @OliverJAsh

My builds are throwing the following errors after upgrades to latest version of the @types/express package:

Property 'headers' does not exist on type 'Request<any>'

Property 'query' does not exist on type 'Request<any>'

Property 'body' does not exist on type 'Request<any>'

We have changed nothing on our end. I also noticed that my TS environment, when I open up the index.d.ts of @types/express shows the following errors:

`express-serve-static-core` has no exported member 'ParamsDictionary'

`express-serve-static-core` has no exported member 'Params'

`express-serve-static-core` has no exported member 'Params'

Some kind of breaking change seems to have been introduced, and I cannot seem to find any guidelines on how to address.

Most helpful comment

I solved this issue with yarn by doing:

$ rm -f yarn.lock
$ yarn cache clean
$ yarn

Have nice day!

All 30 comments

Uninstall @types/express and install @types/express-serve-static-core instead , then just restart typescript compiler.

Same problem here! It works in 4.17.1 but not in 4.17.2, so that supposedly patch-level update is breaking.

@ghostganz @duro Could you provide some steps to reproduce the problem?

This file works with 4.17.1:

import { Request } from 'express';

function isHealthCheck(req: Request): boolean {
  return req.method === 'GET' && req.query.healthCheck === 'true';
}

export default isHealthCheck;

With 4.17.2 we get these compile errors:

TS2339: Property 'method' does not exist on type 'Request<any>'.
TS2339: Property 'query' does not exist on type 'Request<any>'.

express-serve-static-core has no exported member 'ParamsDictionary'

express-serve-static-core has no exported member 'Params'

express-serve-static-core has no exported member 'Params'

This suggests you need to upgrade @types/express-serve-static-core, which is a dependency of @types/express.

I had the same issue and can confirm updating @types/express-serve-static-core to 4.16.11 fixed the issue for me. My package-lock file had locked the version to 4.16.7 previously.

With 4.17.2 we get these compile errors:

TS2339: Property 'method' does not exist on type 'Request<any>'.
TS2339: Property 'query' does not exist on type 'Request<any>'.

In my case, it works with @types/express 4.17.2, too.

@duro @ghostganz Could you delete node_modules, package-lock.json, yarn.lock and try again?
If it doesn't work, please paste your package.json.

This simplest package.json works with yarn install , but not work with npm install or yarn install --flat .

package.json { "dependencies": { "@types/express": "4.17.2", "@types/express-serve-static-core": "4.16.8", "@types/node": "^12.12.5", "typescript": "^3.6.4" } }

Because @types/express 4.17.2 (#38887) is depend on @types/express-serve-static-core 4.16.9 (#37718) to provide generic usage.

I got the same problem, just updated to 4.17.2 and everything broke.

I did what @rockwyc992 asked about removing package-lock and node_modules, it fixes the problem

Same problem jumping from 4.17.1 (where everything was working) to 4.17.2

Same. Running rm -rf node modules && rm package-lock.json && npm update fixed that issue, but now all of my req.params are forced into strings.

Definitely a breaking change and not a patch.

Same problem between versions 4.17.1 and 4.17.2

I fixed this in my codebase by temporarily pinning express to 4.17.0:

"dependencies": { "express": "4.17.0" }, "devDependencies": { "@types/express": "4.17.0" }

I had the same issue and solved it with "skipLibCheck": true added to tsconfig.json

Definitely a breaking change and not a patch.

@timshannon I agree with you, but we need to follow the MINOR version with Express.js.
Maybe we could keep this change util Express.js released new version 4.18.

It's a difficult work. I make this pull request (#38478) start at 09/19 , and it's finally merged at 11/02.
I have no time to track this whole month long, maybe more longer than I thought, just to keep this change don't breaking any exist codes.

Sorry about making you all bump version of @types/express , but it's worth to make Express.js more close with TypeScript and make your code more stable with Type Checking.

Why is not the required version of @types/express-serve-static-core updated in @types/express if it only works with the latest version?

Why is not the required version of @types/express-serve-static-core updated in @types/express if it only works with the latest version?

The package.json of @types/express is generated by @typescript-bot automatically.

@Pajn Could we modify that as we need? If it can, please make a new pull request, thx.

Could we modify that as we need?

I have no idea, but if we can't that seems like a severe limitation. This isn't the first time a patch update in @types packes causes massive problem.

@DanielRosenwasser @sandersn Do you know if it would be possible for @types/express to specify a minimum version of @types/express-serve-static-core?

it work for me yarn remove express-serve-static-core

This error still persist and I'm getting it for ^4.17.0 too.

Found it.
The problem is with the lock file. You simply need to delete the lines refering express-serve-static-file and relaunch a yarn or npm i.

Within my yarn.lock file, this is what I had after a @types/express upgrade to 4.17.2 :

"@types/express-serve-static-core@*":
  version "4.16.7"
  resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.16.7.tgz#50ba6f8a691c08a3dd9fa7fba25ef3133d298049"
  integrity sha512-847KvL8Q1y3TtFLRTXcVakErLJQgdpFSaq+k043xefz9raEf0C7HalpSY7OW5PyjCnY8P7bPW5t/Co9qqp+USg==
  dependencies:
    "@types/node" "*"
    "@types/range-parser" "*"

"@types/express@^4.17.2":
  version "4.17.2"
  resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.2.tgz#a0fb7a23d8855bac31bc01d5a58cadd9b2173e6c"
  integrity sha512-5mHFNyavtLoJmnusB8OKJ5bshSzw+qkMIBAobLrIM48HJvunFva9mOa6aBwh64lBFyNwBbs0xiEFuj4eU/NjCA==
  dependencies:
    "@types/body-parser" "*"
    "@types/express-serve-static-core" "*"
    "@types/serve-static" "*"

You can see the version mismatch.
So I deleted the first block, yarn again, and this is what I have now :

"@types/express-serve-static-core@*":
  version "4.17.2"
  resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.2.tgz#f6f41fa35d42e79dbf6610eccbb2637e6008a0cf"
  integrity sha512-El9yMpctM6tORDAiBwZVLMcxoTMcqqRO9dVyYcn7ycLWbvR8klrDn8CAOwRfZujZtWD7yS/mshTdz43jMOejbg==
  dependencies:
    "@types/node" "*"
    "@types/range-parser" "*"

"@types/express@^4.17.2":
  version "4.17.2"
  resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.2.tgz#a0fb7a23d8855bac31bc01d5a58cadd9b2173e6c"
  integrity sha512-5mHFNyavtLoJmnusB8OKJ5bshSzw+qkMIBAobLrIM48HJvunFva9mOa6aBwh64lBFyNwBbs0xiEFuj4eU/NjCA==
  dependencies:
    "@types/body-parser" "*"
    "@types/express-serve-static-core" "*"
    "@types/serve-static" "*"

No more mismatch, and no more error.

I got it to work after running yarn remove on all express-related types (including express-jwt) and re-adding them afterwards.

I solved this issue with yarn by doing:

$ rm -f yarn.lock
$ yarn cache clean
$ yarn

Have nice day!

For me, it was express-serve-static-core which causing the problem, updated to the latest version of express and @types/express, removed express-serve-static-core && @types/express-serve-static-core, and worked for me.

In the beginning, I thought it was due to body-parser, but Nope.

For Windows, npm and VS code with automatic type acquisition I fixed this by going to %localappdata%\Microsoft\TypeScript\, go into the latest version folder, eg: 3.9 and run npm install @types/express-serve-static-core

I not sure why @types/express-serve-static-core is not installed there for my main machine which has been using typescript for quite awhile. But for my another fresh machine, it is already installed there.

Update:
I thinking running npm update there also works.

this is still an issue in 2020. I mean there is a lot of things wrong in 2020 but this has been persisting since 2017 like come on guys

@xtianus79 TBH, koa and fastify are much better framework choices in 2020. Express had a good run, but now it's an obviously legacy lib.

The answer for me was explicitly installing @types/express-serve-static-core in addition to @types/express.

From my package.json:

    "@types/express": "^4.17.6",
    "@types/express-serve-static-core": "~4.17.7"

I received the following errors when upgrading @types/express from "4.17.2" to "4.17.6":

error TS2694: Namespace '"@types/express-serve-static-core/index"' has no exported member 'Query'.
error TS2707: Generic type 'ErrorRequestHandler' requires between 0 and 3 type arguments.
error TS2694: Namespace '"@types/express-serve-static-core/index"' has no exported member 'Query'.
error TS2707: Generic type 'Request<P, ResBody, ReqBody>' requires between 0 and 3 type arguments.
error TS2694: Namespace '"@types/express-serve-static-core/index"' has no exported member 'Query'.
error TS2707: Generic type 'RequestHandler<P, ResBody, ReqBody>' requires between 0 and 3 type arguments.

I called yarn why @types/express-serve-static-core and checked which version I have:

info Reasons this module exists
   - "@types#express" depends on it
   - Hoisted from "@types#express#@types#express-serve-static-core"

Running yarn upgrade --latest in my project helped because it updated the underlying version of "@types/express-serve-static-core" in yarn.lock.

This is still an issue, installed the latest express / express-serve-static-core types. The problem is when extending the Request interface.

interface ExampleRequest extends Request {
  example: any
}
// No overload matches this call...
router.use('/api', async function (req:ExampleRequest, res, next) { ... }
Was this page helpful?
0 / 5 - 0 ratings