I'm using jsonwebtoken
to decode a token, and I'm trying to get the expiration date. Typescript is throwing errors regarding the exp
property, and I'm not quite sure how to solve them:
import jwt from 'jsonwebtoken'
const tokenBase64 = 'ey...' /* some valid token */
const token = jwt.decode(tokenBase64)
const tokenExpirationDate = token.exp
// ^^^
// Property 'exp' does not exist on type 'string | object'. Property 'exp' does not exist on type 'string'.
I have installed @types/jsonwebtoken
, and looked for a token type to cast token
, but did not find any. Suggestions?
Using
.tsconfig:
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"jsx": "Preserve",
"moduleResolution": "Node",
"module": "ESNext",
"sourceMap": true,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"target": "ESNext"
}
}
import jwt from 'jsonwebtoken'
. I updated my answer accordingly. Hope it will work for you. If you get an error that require can't be found donpm install @types/node --save-dev
, see #31174238 – Joy