I have a code snippet like following:
const user_jwt = jwt.sign(
{user_id : user_id.uuid},
process.env.JWT_KEY as string,
{
expiresIn: process.env.JWT_EXPIRES_IN,
},
);
But I get the following error for the sign()
method:
No overload matches this call.
Overload 1 of 5, '(payload: string | object | Buffer<ArrayBufferLike>, secretOrPrivateKey: null, options?: (SignOptions & { algorithm: "none"; }) | undefined): string', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'null'.
Overload 2 of 5, '(payload: string | object | Buffer<ArrayBufferLike>, secretOrPrivateKey: Buffer<ArrayBufferLike> | Secret | PrivateKeyInput | JsonWebKeyInput, options?: SignOptions | undefined): string', gave the following error.
Type 'string' is not assignable to type 'number | StringValue | undefined'.
Overload 3 of 5, '(payload: string | object | Buffer<ArrayBufferLike>, secretOrPrivateKey: Buffer<ArrayBufferLike> | Secret | PrivateKeyInput | JsonWebKeyInput, callback: SignCallback): void', gave the following error.
Object literal may only specify known properties, and 'expiresIn' does not exist in type
I have tried to edit it in different ways like
expiresIn: (process.env.JWT_EXPIRES_IN ?? "100d") as string,
or defining expiresIn
separately like
const expiresIn = getEnvVar("JWT_EXPIRES_IN", "100d");
but none of them worked.
This code was working before I updated all my nodejs packages. And now it will work only if I hardcode it like expiresIn: "100d"
And this is inside my .env
file:
JWT_EXPIRES_IN=100d
The type definition for expiresIn
says:
expiresIn?: StringValue | number;
(recently started using types from ms
)
So, you could do:
expiresIn: process.env.JWT_EXPIRES_IN as ms.StringValue; // or also: as unknown as number;
or assert the object as SignOptions
:
{
expiresIn: process.env.JWT_EXPIRES_IN,
} as SignOptions