question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typescript verify unable to get data from decoded

See original GitHub issue

Here 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:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

15reactions
luisgurmendezMLabscommented, Dec 18, 2019

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 like verify<DecodedParams>(...): DecodedParams

7reactions
tgiardinacommented, Aug 16, 2020

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.

const decoded = <any>verify(token, secret);

But I think @luisgurmendezMLabs has a point: It would be much more intuitive if the previous code could be rewritten:

const decoded = verify<IDecoded>(token, secret);

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found