0

I'm trying to add express-session to my Node.js application which is running under the Docker.

Read a lot of posts:

Express Session: Property 'signin' does not exist on type 'Session & Partial<SessionData>'. (2339) https://github.com/DefinitelyTyped/DefinitelyTyped/issues/49941 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/46861

I've tried to do a declaration merging of Session property, here is how my tsconfig.json looks like:

"typeRoots": [
  "./src/types",
  "./node_modules/@types"
]

In my src/types folder I have index.d.ts file:

declare module 'express-session' {
 interface Session {
    user: string;
  }
}

But when I run my project with docker-compose up command it gives me the following error:

/app/node_modules/ts-node/src/index.ts:421
     return new TSError(diagnosticText, diagnosticCodes)
            ^
 TSError: ⨯ Unable to compile TypeScript:
 src/routes/auth.ts(26,17): error TS2339: Property 'user' does not exist on type 'Session & Partial<SessionData>'.
 
     at createTSError (/app/node_modules/ts-node/src/index.ts:421:12

I don't think it is a Docker issue cause it happens during Node.js app boot. Does anyone has any idea how this could be resolved?

Karen
  • 1,249
  • 4
  • 23
  • 46

3 Answers3

0

You should extend SessionData interface, NOT Session. There is no Session interface in the index.d.ts file of @types/express-session.

declare module 'express-session' {
  interface SessionData {
    user: string;
  }
}
Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • I have the same issue and have tried this, but ts-node still throws an error. tsc and ts-lint accept the declaration but for some reason ts-node does not. – MoSheikh Jan 22 '21 at 08:33
0

Additionally to the previous answer, that you have to extend the SessionData interface, you might use the --files flag for ts-node. Described in this answer post. Right now it seems that ts-node doesn't take your tsconfig.json.

Max
  • 1,203
  • 1
  • 14
  • 28
0

To extend the express-session types, (as of @types/express-session@^1.17.4), you need to add:

import "express-session"

to the top of your d.ts file: i.e:

import "express-session"; 

declare module 'express-session' {
  interface SessionData {
    tenant: string
  }
}

Ensure also, as @slideshowp2 has said, that you are extending SessionData not Session

Jamie
  • 3,105
  • 1
  • 25
  • 35