I am trying to enhance the Express Session typings to incorporate my custom data in session storage. In my code, I have an object req.session.user
which is an instance of a class called User
:
export class User {
public login: string;
public hashedPassword: string;
constructor(login?: string, password?: string) {
this.login = login || "" ;
this.hashedPassword = password ? UserHelper.hashPassword(password) : "";
}
}
To achieve this, I created my own definition file named own.d.ts
to merge definitions with existing express session typings:
import { User } from "./models/user";
declare module Express {
export interface Session {
user: User;
}
}
However, it seems that it's not functioning properly - both VS Code and tsc are unable to detect it. To test, I added a simple type field as follows:
declare module Express {
export interface Session {
test: string;
}
}
Surprisingly, the test field works fine while importing the User class causes issues.
I also attempted to use
/// <reference path='models/user.ts'/>
instead of import, but the tsc did not recognize the User class. How can I successfully utilize my own class in a *d.ts file?
EDIT: After configuring tsc to generate definition files during compile, I now have my user.d.ts:
export declare class User {
login: string;
hashedPassword: string;
constructor();
constructor(login: string, password: string);
}
Additionally, here is the typing file for extending Express Session:
import { User } from "./models/user";
declare module Express {
export interface Session {
user: User;
uuid: string;
}
}
Despite these changes, the issue persists when using the import statement at the top. Any suggestions?