typescript verify unable to get data from decoded
See original GitHub issueHere is how I use the method:
const decoded = jwt.verify(req.body.jwtToken, process.env.JWTSECRET);
return res.send({
user: decoded.user // <-- error here
});
and I have a linter error : "Property user does not exists on type “obejct|string”
Issue Analytics
- State:
- Created 5 years ago
- Comments:7
Top Results From Across the Web
typescript jwt.verify cannot access data - Stack Overflow
My problem is that I can't read the data from the JWT verify function. I'm using it like this : //encode when logging...
Read more >Json decoders in TypeScript - JavaScript Christmas
Well you can't really do that easily, but you can verify that the data actually does match the type you say it has,...
Read more >Methods for TypeScript runtime type checking - LogRocket Blog
Explore five methods of performing TypeScript type checks at runtime in this post and learn each of their advantages and disadvantages.
Read more >Notes on Advanced TypeScript: Runtime Validations
Via leveraging this functionality we can decode external data and handle the success/failure case specifically. To get a better ...
Read more >TypeScript: JavaScript With Syntax For Types.
TypeScript extends JavaScript by adding types to the language. TypeScript speeds up your development experience by catching errors and providing fixes ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think that casting the decoded as any is a good solution. In any case the best thing will be to create an interface and cast it to that.
In the other hand I think that this library has a poor typing implementation. The verify function should be typed. So
verify
should be something likeverify<DecodedParams>(...): DecodedParams
This is the first thing that pops up when you Google “jsonwebtoken verify typescript,” so I think it deserves a clearer conclusion.
Casting like @ghost suggested works, e.g.
But I think @luisgurmendezMLabs has a point: It would be much more intuitive if the previous code could be rewritten:
It looks like @zRelux never had time to complete his pull request, so as of now, the first way is currently the only way.
Someone please jump in if I got any of the above wrong. Also, if there’s some interest, I could maybe look into implementing the second way.