I am trying to add some properties to the Express/Request object, and everything seems fine but when I try to compile with tsc I get this error: error TS2339: Property 'id' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
I've tried all the answers found in this post Extend Express Request object using Typescript, but none of them seem to work for me.
In my root folder I created this:
@types/express/index.d.ts
declare module Express {
export interface Request {
id: number
user_spotify_id: string
access_token: string
refresh_token: string
}
}
I also added the types folder to the tsconfig.json file:
"typeRoots": ["@types", "node_modules/@types"]
This is my callback function for the controller:
async getPlaylists(req: Request, res: Response) {
const { id } = req
const query = `SELECT * FROM playlist
WHERE user_id = $1`
const { rows: playlists }: { rows: IPlaylistAll[] } = await db.query(query, [id])
return res.json(playlists[0])
},
I am able to get the id from the request object, and it even says the type is number, but when compiling I get the error I mentioned before.