question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Session object not modifiable in typescript (while using @types/express-session)

See original GitHub issue

I’ve been using express-session in various express applications, but never with typescript. Disclaimer: This might be a bug with @types/express-session, but I figure this is the right community to ask either way, so here’s what’s happening.

The session object is essentially just not modifiable, ie I can’t seem to add properties to it, and, perhaps because of a recent update, all methods of fixing this I’ve found either don’t work or just break the whole thing.

In this example use case:

router.get("/example", (req, res) => {
	console.log(req.session); // Works
	req.session.username = "mine" // Fails
	req.session.destroy(data => console.log(data)); // Works
	res.send('something')
});

I get the following error:

Property 'username' does not exist on type 'Session & Partial<SessionData>'

Not sure if this is in any way useful, but these are my session options

const sessionConfig: session.SessionOptions = {
	name: "uid",
	secret,
	store: new RedisStore({
		client: redisClient,
		disableTTL: true,
		disableTouch: true,
	}),
	cookie: {
		maxAge: 1000000000000999,
		httpOnly: true,
		sameSite: "lax",
		secure: __prod__,
	},
	saveUninitialized: false,
	resave: false,
};

I have attempted to create a separate type combining express.Request and the session options, but I can’t really get that to work either, and I’m assuming there’s a bit more elegant way of dealing with this.

Thank your for your time.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
jasonlimantorocommented, Jan 5, 2021
  1. Make sure your tsconfig.json’s typeroots declares custom types first, before the node_modules’s.
"typeRoots": ["./src/typings", "./node_modules/@types"]
  1. Declare definition file
// ./src/typings/express-session/index.d.ts
import "express-session"; // don't forget to import the original module

declare module "express-session" {
    interface SessionData {
        username: string // whatever property you like
    }
}

That should be enough, hope this helps everyone who is stuck on Typescript hole 😄

14reactions
randomiconscommented, Dec 13, 2020

Add this to a file in your project ( ie types.ts etc.)

declare module "express-session" {
  interface SessionData {
    username: string
  }
}

You can add all of your custom fields in session inside of the SessionData interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use @types/express-session? - Stack Overflow
tsc gives me an error: 'Object is possibly 'undefined'. I know the original Request type does not have the session field.
Read more >
How to Extend Express Request With Session in Typescript ...
You are trying to add something to the request object which doesn't exist per TypeScript definitions.
Read more >
Session | NestJS - A progressive Node.js framework
A session is uninitialized when it is new but not modified. Choosing false is useful for implementing login sessions, reducing server storage usage,...
Read more >
Extend Express's Request Object with ... - DEV Community ‍ ‍
The above code is an Express middleware that is used to ensure that a user is authenticated when he or she tries to...
Read more >
@holdyourwaffle/express-session - npm
There are no other projects in the npm registry using ... Please use the upstream express-session and @types/express-session instead.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found