'Property `user` does not exist on type `Session & Partial<SessionData>`
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.
Solution 1:[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:[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:
import "express-session";
declare module "express-session" {
interface SessionData {
user: string;
}
}
Just replace string
with whatever type you need for that field.
Also I have added ./typing-stubs
in tsconfig.json
"typeRoots": [
"./typing-stubs",
"./node_modules/@types"
]
Solution 3:[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 };
};
Solution 4:[4]
This is for future reference, for someone who have done all the step above but keep failing (vscode doesn't show the error anymore but still failed), it maybe ts-node
problem, add --transpile-only
to your package.json
script, like this: nodemon --exec ts-node -T src/app.ts
.
Source: Typescript Declaration Merging fail about Express Request
Solution 5:[5]
Trace the source code you'll find such a comment:
/**
* 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;
}
Go ahead and read the documentation; you'll find a module augmentation section: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
Now, augment "SessionData" yourself and add "user" to it in: src/types/express-session/index.d.ts:
import { SessionData } from "express-session"
declare module "express-session" {
interface SessionData {
user: { [key: string]: any }
}
}
Your tsconfig.json should set types roots to include the declaration in your compilation:
"typeRoots": ["./node_modules/@types", "./src/types"],
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kapobajza |
Solution 2 | Rich |
Solution 3 | |
Solution 4 | Phineas |
Solution 5 | Louis Huang |