Custom type on express Request doesn't work on execution

I'm trying to set a user type on my req in my express server these are what i tried: req.user = user; req.user = user as User; (req as User).user = user; and for the types file:
import { User } from '@prisma/client';

declare global {
namespace Express {
export interface Request {
user?: User;
}
}
}
import { User } from '@prisma/client';

declare global {
namespace Express {
export interface Request {
user?: User;
}
}
}
i tried putting the above in /global.d.ts in /src/types/express.d.ts and /src/typings/express/index.d.ts my editor doesn't error at all - works in all of these solutions but while running yarn dev it errors
src/clients/web/router.ts:66:8 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
src/clients/web/router.ts:66:8 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
this is my tsconfig if that's of any use:
{
"compileOnSave": false,
"compilerOptions": {
"target": "es2022",
"lib": ["es2022", "esnext.asynciterable"],
"typeRoots": ["node_modules/@types"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"declaration": true,
"outDir": "dist",
"allowJs": true,
"noEmit": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"importHelpers": true,
"baseUrl": "src",
"paths": {
"@/*": ["*"],
"@config": ["config"],
"@controllers/*": ["controllers/*"],
"@types/*": ["types/*"],
"@middlewares/*": ["middlewares/*"],
"@routes/*": ["routes/*"],
"@utils/*": ["utils/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.json", ".env", "global.d.ts"],
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
}
{
"compileOnSave": false,
"compilerOptions": {
"target": "es2022",
"lib": ["es2022", "esnext.asynciterable"],
"typeRoots": ["node_modules/@types"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"declaration": true,
"outDir": "dist",
"allowJs": true,
"noEmit": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"importHelpers": true,
"baseUrl": "src",
"paths": {
"@/*": ["*"],
"@config": ["config"],
"@controllers/*": ["controllers/*"],
"@types/*": ["types/*"],
"@middlewares/*": ["middlewares/*"],
"@routes/*": ["routes/*"],
"@utils/*": ["utils/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.json", ".env", "global.d.ts"],
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
}
4 Replies
dan
dan13mo ago
What is happening here is you are not setting the user key on the actual request object that is given to the function at src/clients/web/router.ts:66. You've told typescript that the request type has user on it but in this case you're "lying" to typescript. You need to ensure user is correctly being added to the object (I imagine you have some middleware for authenitcation that adds this to the request).
soul
soul13mo ago
oh right, lemme check if req.user is being set properly
soul
soul13mo ago
looks like it is actually
No description
soul
soul13mo ago
@dan.mkv this didnt work btw i have it ts-ignored for now do u know any solution to this? i can give you more details i have a very similar issue with telegraf as well right now when i console.log ctx.message there's a property called text which is printed out but when i try to try to access it in code like const message = ctx.message.text; - i get an error saying text doesnt exist
Want results from more Discord servers?
Add your server