skip to Main Content

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:

  1. As comments show in the code, different import statements like express and/or session.

  2. Changing strictNullChecks from true to false in package.json.

  3. The latest versions of express, express-session, @types/express and @types/express-session.

  4. Change type to interface.

  5. Double checked similar questions before posting this (I had already looked here on SO and Google).

  6. Comparing the tutorial’s final code on GitHub vs my own code (they’re the same as far as I can tell).

2

Answers


  1. Chosen as BEST ANSWER

    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 via req.session.userId, so that's added (wasn't in original question).

    import { Request, Response } from "express";
    import { Session, SessionData } from "express-session";
    import { Redis } from "ioredis";
    
    export type MyContext = {
        req: Request & {
            session: Session & Partial<SessionData> & { userId: number };
        };
        redis: Redis;
        res: Response;
    };
    

  2. 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 called Express 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 the express-session library:

    import { Request, Response } from "express";
    import { Redis } from "ioredis";
    import session from "express-session";
    
    export type MyContext = {
        req: Request & { session: session.Session };
        redis: Redis;
        res: Response;
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search