I've been working on building a website for the past couple of days and decided to use TypeScript.
For my web server, I opted for Express.js, which I've used in the past and am familiar with.
One thing I noticed about TypeScript is that it scrutinizes each property closely. However, I encountered an issue with the "user" property not being set by default, which I wanted to utilize for saving decoded user sessions.
After some online research, I came across the following solution:
declare module "express" {
export interface Request {
user: any
}
}
This information was found in this post: Typescript Error: Property 'user' does not exist on type 'Request'
I implemented this code into a file and it seems to work fine, but I have encountered two issues:
- I'm unsure why it works without requiring the file directly. Instead, I included it in typeroots within tsconfig.json and executed it with ts-node. While Visual Studio Code doesn't show an error, ts-node does.
- I tried to assign the type Session to the user property after importing Session into the file. However, Visual Studio Code now indicates that the User property does not contain it.