[SOLVED] 'typeRoots' fails to find d.ts declaration files in project

Issue

// tsconfig.json inside "compilerOptions"
    "typeRoots": ["./@types", "./node_modules/@types"],

// {projectRoot}/@types/express/index.d.ts
declare global {
  namespace Express {
    interface Request {
      verifiedAccessToken: string
    }
  }
}

In another file with req annotated to Request inside the function parameter, I am still not able to set verifiedAccessToken without a lint error in VsCode:

req.verifiedAccessToken = accessToken;
// Property 'verifiedAccessToken' does not exist on 
// type 'Request<ParamsDictionary, any, any, ParsedQs>'.ts(2339)

Anything else that needs to be done for the declaration file to be used?

// package.json, relevent deps
 "dependencies": {
    "express": "^4.17.1",
    "node-typescript": "^0.1.3",
    "ts-node": "^9.0.0",
    "typescript": "^3.9.7"
  },
  "devDependencies": {
    "@types/express": "^4.17.8",
    "@types/node": "^14.0.27",

Solution

Try adding this to your tsconfig.json:

{
    "compilerOptions": {
        "baseUrl": "./",
        "paths": {
            "*" : ["@types/*"]
        }
    }
}`

See: https://github.com/microsoft/TypeScript/issues/22217

Answered By – dwosk

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *