panva / jose

JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes
MIT License
5.62k stars 315 forks source link

SignJWT exp type error #555

Closed F2BEAR closed 1 year ago

F2BEAR commented 1 year ago

What happened?

I'm trying to generate a JWT using the exp from user's prompt for the SignJWT class which in the docs is stated that it's type is string | number but Typescript is displaying an error stating that the exp type is number | undefined preventing me from using strings like 1h or 2.5 weeks as the exp value.

Version

v4.14.4

Runtime

Browser

Runtime Details

v18.16.0

Code to reproduce

let expiration: string | number = await input({message: "When is the expiration date?"})
if (/^\d+$/.test(expiration) === true) {
    expiration = parseInt(expiration)
}
jwt = await new SignJWT({
    iss: basicAnswer.issuer,
    aud: basicAnswer.audience,
    sub: basicAnswer.subject,
    exp: expiration,  // Type 'string | number' is not assignable to type 'number | undefined'.
    ...payload
})
.setProtectedHeader({alg, typ})
.setIssuedAt()
.sign(secret)

Required

panva commented 1 year ago

preventing me from using strings like 1h or 2.5 weeks as the exp value.

That's not how the payload you can pass to the constructor is working. That's for the instance method setExpirationTime.

which in the docs is stated that it's type is string | number

Where does it state that? The docs are generated from the types so I doubt that's what's in the docs for the payload argument.

F2BEAR commented 1 year ago

Damn, you are right.

I expected the exp value for the constructor to be of the same type as the one on the setExpirationTime method for no reason; my bad.

Feeling kinda dummy now, sorry for the trouble.