1

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.

Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42

1 Answers1

-1

Look, I was with this problem, I managed to solve it by changing the --transpileOnly flag to --transpile-only in package.json, check the version you are using of TypeScript, mine is 3.9.6.

And it was not necessary to configure tsconfig.json for min.