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:
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
this is my tsconfig if that's of any use:
4 Replies
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).oh
right, lemme check if req.user is being set properly
looks like it is actually
@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