Open benbai123 opened 2 years ago
specify dependency "@types/jsonwebtoken": "8.5.4",
in your package.json
We can fix the error by either using const jwt = require('jsonwebtoken');
Or since Typescript cannot infer the correct type and therefore exp
and other items available in jwt.payload
are not known, the simplest way out is to cast the result to any
in this manner const token: any = jwt.decode()
We can fix the error by either using
const jwt = require('jsonwebtoken');
Or since Typescript cannot infer the correct type and therefore
exp
and other items available injwt.payload
are not known, the simplest way out is to cast the result toany
in this mannerconst token: any = jwt.decode()
Using any
type defeats the purpose of using Typescript and should be avoided.
I worked around this by using a typeof
expression checking if the type is not a string, but rather a JwtPayload. But looks like it returns a string, why? Is there a way to force JWT to only return a payload?
this problem still exists. In docs it says
(Synchronous) If a callback is not supplied, function acts synchronously. Returns the payload decoded if the signature is valid and optional expiration, audience, or issuer are valid. If not, it will throw the error.
But when I try to access payloads properties, ts error accrues arguing about property doesn't exists on type string.
Isn't this more of a problem with the @types/jsonwebtoken
package than jsonwebtoken
itself? It seems like the types package is saying that the type returned by decode
can be string | JwtPayload
but I don't see any indication that a string will be returned by decode
. It actually does a type check for string and tries to convert strings to objects.
You could probably safely assert as JwtPayload
, otherwise type checking for a string would be the other TypeScript-friendly solution.
We should probably file this as an issue on the DefinitelyTyped repo.
Description
@types/jsonwebtoken
8.5.8 break the nestjs application,sample code
error message:
Reproduction
"jsonwebtoken": "^8.5.1",
to your package.jsonEnvironment