I’m delving into a React, Express and Typescript tutorial, to which I’m still pretty new to. Trying to better understand how setting up custom types works, and this is what I currently have.
In types.ts file:
import { Request, Response } from "express";
import { Redis } from "ioredis";
//import express from "express"; // adding this did not help
//import session from "express-session"; // adding this did not help
export type MyContext = {
req: Request; // & { session: Express.Session }; // this is my troublesome line
redis: Redis;
res: Response;
};
When trying to add
& { session: Express.Session }
I get the error:
any Namespace ‘global.Express’ has no exported member
‘Session’.ts(2694)
Versions of devDepencies and also depencies have been compared so that they’re the same. Trying the latest versions had no change.
devDependencies:
"@types/express": "^4.17.7",
"@types/express-session": "^1.17.0",
...
dependencies:
"express": "^4.17.1",
"express-session": "^1.17.1",
...
Things I’ve tried:
-
As comments show in the code, different import statements like express and/or session.
-
Changing strictNullChecks from true to false in package.json.
-
The latest versions of express, express-session, @types/express and @types/express-session.
-
Change type to interface.
-
Double checked similar questions before posting this (I had already looked here on SO and Google).
-
Comparing the tutorial’s final code on GitHub vs my own code (they’re the same as far as I can tell).
2
Answers
I believe the problem stemmed from different versions of a dependency used by
express-session
. I found the tutorial and this problem being discussed elsewhere (not on SO) but was able to make the changes below.req.session
is available throughout my project now. In my use case, I wanted userId accessible viareq.session.userId
, so that's added (wasn't in original question).The error you are getting tells you that it’s looking for a type in
global.Express.Session
. This is because you have no import calledExpress
so it’s trying to find it on the global scope. Of course, this does not exist!This worked for me, assuming that the
Session
type was supposed to come from theexpress-session
library: