9

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

Reza Razani
  • 93
  • 1
  • 7

3 Answers3

7

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

Matt Knight
  • 469
  • 1
  • 6
  • 17
5

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.

BenHu
  • 246
  • 2
  • 6
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
    }
  }
}
Timothy Johns
  • 995
  • 5
  • 16