Property `user` does not exist on type `Session & Partial<SessionData>`

11,234

Solution 1

As stated in the express-session types comment, you have to use Declaration merging.

Here's how you can implement Declaration merging on express-session:

import session from 'express-session';

declare module 'express-session' {
  export interface SessionData {
    user: { [key: string]: any };
  }
}

Solution 2

I just encountered the same issue as you. This seems to be a fairly recent issue: see explanation here.

To fix this I overloaded the module as described in the github issue:

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

Just replace string with whatever type you need for that field.

Solution 3

i had this issue recently and here is the solution i came up with.

import { Request } from "express";
import { Session } from "express-session";

export type SessionWithUser = Session & { user: string | {}};

export type AuthRequest = Request & {
  session?: SessionWithUser;
  auth?: { user: string; permission_id: number };
};
Share:
11,234
amir yeganeh
Author by

amir yeganeh

Updated on June 25, 2022

Comments

  • amir yeganeh
    amir yeganeh almost 2 years

    I had a code in javascript and I'm trying to convert it to typescript

    route.get('/order', async(req,res) => {  
        var sessionData = req.session;
        if(typeof sessionData.user === 'undefined')
        {        
            res.redirect('/panel/login');
        }    
    

    this is a piece of my code that used to work correctly in javascript but now I get this error for the user:

    Property 'user' does not exist on type 'Session & Partial'

    I assume I should add types for the sessionData variable and (req, res) params but I don't know what type should exactly be assigned to it.

    PS: I know this question looks duplicated but I've tried solutions from other similar questions and it didn't work

    any help would be appreciated.

  • amir yeganeh
    amir yeganeh over 3 years
    how should i do it? i mean should i write this code in a new file and import it to my main file ?
  • Rabbit
    Rabbit over 3 years
    I'm not very familiar with ts; what I did is just put this in the same file where I do the app.use(session(....)) and it worked. It's likely not the best way to do this but it fixed my issue.
  • Emre Dalkiran
    Emre Dalkiran over 3 years
    tsconfig.json has a field named typeRoots, which is commented out by default. You can uncomment that field and add the folder/file where you define new types. Don't forget to add node_modules/@types since this is the default directory for where installed @type packages are located.
  • amir yeganeh
    amir yeganeh over 3 years
    why don't I see any typeRoots? should I add it manually?
  • tomasantunes
    tomasantunes about 3 years
    Please provide some explanation.
  • Admin
    Admin about 3 years
    create a ts file and copy the code above. then import it wherever you need it and set the req type to it.
  • Titus Sutio Fanpula
    Titus Sutio Fanpula almost 3 years
    Awesome. Thanks
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • akristmann
    akristmann over 2 years
    A special note to all that are using ts-node-dev. Take care if you add this in a new file that is not directly included in your entry file. ts-node-dev won't pick up any type changes while it's currently running. In fact it only compiles files that are included in the entry file and it works its way through the tree based on the imports/exports, so it doesn't know about types that aren't directly included in your project. tsc had no problem compiling and finding the new type, so from now on I'm using ts-node-dev only with the --transpile-only parameter and let tsc do the typechecking.
  • Jamie
    Jamie over 2 years
    Excellent answer. I think the important thing to flag is the use of import "express-session" at the top of the declaration file.