SolvedDefinitelyTyped Incorrect typings for session object

Adding a new property to the sessions object throws an error.
For example doing this: req.session.userId throws an error Property 'userId' does not exist on type 'Session & Partial<SessionData>'.

21 Answers

✔️Accepted Answer

@wenhx here is how I did it.

First, I edited tsconfig.json to include custom defined types.

{
//other config options

  "typeRoots": [
       "./src/types", // this is where you define your types
       "./node_modules/@types" //this where npm packages containing types are located
     ]

   //other config options
 }

Within the file index.ts which is under src/types directory:

import { Session } from 'express-session'

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

I was able access all session properties plus the one I defined under request.session object.

Alternatively, instead of Session, you can import SessionData and change interface name to SessionData, it also works like that. I needed the session else where as a type, therefore I merged with Session.

Downgrading to previous @types/express-session module didn't make sense to me because I've read comments of the maintainers of the library and this is not a bug but an intended breaking change, which will be the default behavior from now on.

Other Answers:

@BenoitGour take a look at the message at this link which is under the merge request for this change.

You can also inspect index.d.ts file under node_modules/@types/express-session where they provide this information in the comments section if your @types/express-session package is version 1.17.1 or newer.

    /**
     * This interface allows you to declare additional properties on your session object using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html).
     *
     * @example
     * declare module 'express-session' {
     *     interface SessionData {
     *         views: number;
     *     }
     * }
     *
     */
    interface SessionData {
        cookie: Cookie;
    }

I've had to go back to "@types/express-session": "1.17.0" to resolve this issue. The SessionData interface changed from

  interface SessionData {
      [key: string]: any;
      cookie: SessionCookieData;
    }

to

interface SessionData {
        cookie: Cookie;
    }

Without [key: string]: any; additional properties added by the application will throw errors.

I found a solution here:

// Example of adding additional properties to SessionData using declaration merging

But if I put these codes in a separate file it won't work.
After I added two lines of code, it worked, but I don't know why.
Can anyone tell me?

import session from "express-session";

export = session;

declare module 'express-session' {
    interface SessionData {
        views: number;
    }
}

Ran into this issue today trying to use express-socket.io-session and similar packages.
Caught me off guard since I was following the example code provided exactly, only with typescript enabled and took me a long time to narrow it down.

Would be nice if there was an explicit typescript section on these example code sections that said something to the effect of 'if you plan on using TS with this package, please note that you should explicitly declare the expected types on this object, as newer versions no longer allow [key: string]: any;'
And show a nice clean example...etc

I can imagine myself liking the declaration merging, seems safe and well intended.

EDIT: I'm using ts-node-dev and by setting the --files flag, it made everything work!

More Issues: