Namespace 'Express' has no exported member 'SessionData'
Asked Answered
E

4

12

I'm trying to build a typeScript project but it has an error and I do not know where it comes from and nothing has changed since last time.


node_modules/connect-mongo/src/types.d.ts:113:66 - error TS2694: Namespace 'Express' has no exported member 'SessionData'.

113         get: (sid: string, callback: (err: any, session: Express.SessionData | null) => void) => void;
                                                                     ~~~~~~~~~~~

node_modules/connect-mongo/src/types.d.ts:114:45 - error TS2694: Namespace 'Express' has no exported member 'SessionData'.

114         set: (sid: string, session: Express.SessionData, callback?: (err: any) => void) => void;
                                                ~~~~~~~~~~~

node_modules/connect-mongo/src/types.d.ts:118:47 - error TS2694: Namespace 'Express' has no exported member 'SessionData'.

118         touch: (sid: string, session: Express.SessionData, callback?: (err: any) => void) => void;
                                                  ~~~~~~~~~~~

src/controllers/http/auth/auth.ts:16:44 - error TS2339: Property 'user' does not exist on type 'Session & Partial<SessionData>'.

16             if (req.session && req.session.user) {
                                              ~~~~

src/controllers/http/auth/auth.ts:41:29 - error TS2339: Property 'user' does not exist on type 'Session & Partial<SessionData>'.

41                 req.session.user = user;
                               ~~~~

The place where the typescript gives an error

express-session this is how it is imported

Euhemerize answered 15/11, 2020 at 13:47 Comment(3)
How are you importing it? Can you share that snip of code please – Malina
Yes I added i.sstatic.net/HnP6K.png – Euhemerize
same problem here – Skiing
G
10

I reverted back to this version to fix the issue: "@types/express-session": "1.15.16",

Gasometer answered 17/11, 2020 at 14:52 Comment(1)
πŸ‘ Reverting to 1.70.0 also seems to work – Taphole
P
6

For the connect-mongo lib type check, try to add skipLibCheck option to your tsconfig.json

{
  "compilerOptions": {
    ....
    "skipLibCheck": true
  }
}

For the inline code, try to add // @ts-ignore

// @ts-ignore
req.session.user = user;

Please note that it is just a workaround, I have encountered same issue today when I try to rebuild the docker image on the DockerHub without any code changed.

Pyrrhic answered 16/11, 2020 at 9:45 Comment(1)
Thank you: // @ts-ignore fixed my issue. – Histrionic
T
2

It appears the SessionData interface was removed from the global Express namespace, at the former line 24 on October 13, and recently published in the corresponding DefinitelyTyped modules -

https://github.com/HoldYourWaffle/DefinitelyTyped/commit/0cec4865fe7fd873952fc6901553a96648a7c889#diff-c38f30a0fdd238f104a7392ff3881fa97d8b4497d75e9617163ac1b5fceb75bf

Lots of discussion here - https://github.com/DefinitelyTyped/DefinitelyTyped/pull/46576

I suspect this will be eventually be updated in the connect-mongo code at some point, but in the meantime you can work around it with the skipLibCheck flag mentioned by @benhu, or, if you want to leave that feature on for other libraries, you can add the interface back in within your own code somewhere with something like this:

// WORKAROUND TODO: Remove when the connect-mongo types are updated
declare global {
  namespace Express {
    interface SessionData {
      cookie: any
    }
  }
}
Tourism answered 19/11, 2020 at 22:57 Comment(0)
W
0

It is because express-session is now its own package and session related stuff has been removed from express.

import it using import { SessionData } from "express-session";

and it is true for almost everything related to sessions like Session, SessionData, SessionOptions. now you have to import it through express-session not through express.

Warta answered 7/7, 2022 at 14:37 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.