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.

Extending session object

See original GitHub issue

Hi there, I know this is not an actual issue, but a help wanted.

I’m struggling with typescript and express session and I have been playing around and trying to fiugure out this one for quite a bit.

I’m trying to extend my session object, for that, I’m trying to do a merge of typings as per in the documentation:

I have a types/session.d.ts with the following interface to be merged:

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

But this is not working, as an example, in other-folder/some.ts

req.session.userId = user.id;
// Property 'userId' does not exist on type 'Session & Partial<SessionData>'.

However, if I import Session from express-session, it does work:

import { Session } from 'express-session'

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

I’m not very proficient with TypeScript and I’m unsure about importing a module in a type defintion, event TypeScript complains about this (warning):

‘Session’ is declared but its value is never read.

I’m wondering, is this the right way of tackling the problem?

What can I do different?

Kind regards!

PS: my tsconfig should be fine as I have available through my code other type definitions and they are working with no issues at all.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Jan 20, 2021

I can certainly reopen the issue, but I have no idea about anything Typescript related so cannot provide any answers myself on the topic. The typescript stuff is not even managed in this repository so there is nothing to merge into here to affect change around it, if that is needed.

0reactions
Avcajaravillecommented, Jan 22, 2021

Closing because of lack of input. Thanks @dougwilson 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending session object in express-session - Stack Overflow
I'm trying to extend my session object, for that, I'm trying to do a merge of typings as per in the documentation:.
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. A Possible Solution. We need to make...
Read more >
Using extended runtime sessions - Apple Developer
With background sessions, your app continues to run in the background, but the sessions can only monitor workouts, track the user's location, or...
Read more >
How to use express-session with your custom SessionData ...
Solution 1 - don't do this one. A common fix I see is to extend the express.Request object with a user field: Copy....
Read more >
Advanced Usage — Requests 2.28.1 documentation
Session Objects ¶ · The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made...
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