I want to add types for my response.locals
. It is used to append data to your request-response cycle.
What I tried
// ./types/express/index.d.ts
declare global {
declare namespace Express {
interface Response {
locals: {
userId: number;
};
}
}
}
// tsconfig.json
"compilerOptions": {
"typeRoots": ["./types"],
}
myController.post("/", async (request, response) => {
// Can't get type in my controller
const { userId } = response.locals; // <- userId is any
Goal: Get correct type inference for my response.locals
variable
Versions:
"express": "^4.17.1",
"@types/express": "^4.17.8",
"typescript": "^4.5.4"
typeRoots
option has a default value; make sure to include it – Colenefiles
section of tsconfig? – Tightlipped