TS: Namespace 'global.Express' has no exported member 'Session'

I am a bit stuck here and I’m not sure if anyone will be able to help with how deep I am currently. I am 5hr, 43min. into this video by Ben Awad, where we are switching from MikroORM to TypeORM with a postgres DB.

The errors I am getting trying to run yarn watch is:



(He even gets the const conn one! But gets deleted by deleting the /dist directory??)

Here is the server code for his backend.

The file in question is src/types.ts and this is what I have currently:

import { Request, Response } from 'express';
import { Redis } from 'ioredis';

export type MyContext = {
  req: Request & { session: Express.Session };
  redis: Redis;
  res: Response;
};

Which is EXACTLY what he had at this part of the video. I have this in my IDE freaking out:

Any help is super appreciated! Happy holidays, everyone!

You’re not importing the Express type in that file

Apparently there have been changes in types definition for express-session (SO link).
You should remove caret (^) from @types/express-session in server/package.json to have exact version installed:

"@types/express-session": "1.17.0",
1 Like

Nevermind, I had fixed it! Thanks for the help!

Hi, how did you fix it?

Hey @rstorms, I just ran into the same issue following the same tutorial by Ben Award. If you don’t mind, could you please share the solution with me?

Hey, @vivikyd, I’m still trying to barge through this solution funny enough. I’m unable to start my server from yarn dev after yarn watch. He has it set to read the index.js file directly inside the /dist directory, and I’m not sure how it is working for him.

Hey. Could you share the solution to the session error? Thank You

This is my package.json right now, however, I am stuck about ten seconds after what I posted in this thread.

sooo…
this is my take on this:

import { EntityManager, IDatabaseDriver, Connection } from "@mikro-orm/core";
import { Request, Response } from "express";
import { Session } from "express-session";

export type MyContext = {
  em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>;
  req: Request & { session?: Session & { userId?: number } };
  res: Response;
};

seems to eliminate that error

1 Like

Hello hello,

The code you are talking about is useful for version 1.17.0, now that it is in version 1.17.3 there is a small modification that is made :slight_smile:

You need to do this instead ->

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;
};
// UserID -> as you wrote it  (userId, userid, etc...)

This will fix that issue :slight_smile:

Happy coding :slight_smile:

3 Likes

I am able to fix the type issue in the user resolver. But I am getting this error in ApolloServer context object.

This is the error.

Type 'Request<ParamsDictionary, any, any, ParsedQs>' is not assignable to type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>> & { session: Session & Partial<SessionData> & { ...; }; }'.
  Type 'Request<ParamsDictionary, any, any, ParsedQs>' is not assignable to type '{ session: Session & Partial<SessionData> & { userId: number; }; }'.
    Types of property 'session' are incompatible.
      Type 'Session & Partial<SessionData>' is not assignable to type 'Session & Partial<SessionData> & { userId: number; }'.
        Property 'userId' is missing in type 'Session & Partial<SessionData>' but required in type '{ userId: number; }'.ts(2322)
types.ts(8, 49): 'userId' is declared here.
types.ts(7, 3): The expected type comes from property 'req' which is declared here on type 'RedditDbContext'

If there any guidance to resolve this, will highly appreciate. Thank you very much.

Hello,

You can remove the context call because it has become useless because you declare it just before.

context: ({ req, res }) => ({ em: orm.em, req, res }),

This will fix your issue.

Enjoy :slight_smile:

1 Like

Wow, man. It works. :tada: :tada: :tada:Thank you very much.
If you don’t mind, can you please give me a little explanation here. Then next time I will able to prevent this kind of errors by understanding it.
Thank you very much for your time and consideration.

1 Like

Made an account just to upvote this

1 Like