Adding parameter to existing type using typeRoots

Viewed 27

I am trying to add a typeorm instance to the express request object as described in my other question: Pass ORM object to router in express.

The answer suggests to do this by adding it as Middleware.

app.use((req,res,next)=>{
req.dataSource = appDataSource
next();
});

However I get the error:Property 'dataSource' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339)

To solve this I tried this approach https://stackoverflow.com/a/58957229/4923949. I modified my tsconfig and added:

    "typeRoots": [
      "./src/custom_typings",
      "./node_modules/@types",
    ],

And I added a /src/custom_typings/express.d.ts with the content:

import { DataSource } from "typeorm";

declare namespace Express {
  interface Request {
    dataSource: DataSource;
  }
}

Why am I still getting this error?

1 Answers

Try to add express.d.ts like that

/src/custom_typings/express/express.d.ts

without changing in typeRoots.

that was counsel from comment that helped me

Related