I'm using jsonwebtoken library within TypeScript project. Together with this library, I've imported @types/jsonwebtoken library to provide types.
Within this library jsonwebtoken's function verify
declared as following:
export function verify(
token: string,
secretOrPublicKey: Secret,
options?: VerifyOptions
): object | string;
But I would like to specify which object exactly it returns, not just object | string
for example, an object defined by the following interface:
export interface DecodedJwtToken {
userId: string;
primaryEmail: string;
}
How can I achieve it within my project? Can it be done without type conversion, i.e.
const decodedToken: DecodedJwtToken = verify(token, JWT_PRIVATE_KEY) as DecodedJwtToken;
Thank you in advance.
What you're looking for is module augmentation:
import { Secret, VerifyOptions } from 'jsonwebtoken';
export interface DecodedJwtToken {
userId: string;
primaryEmail: string;
}
declare module 'jsonwebtoken' {
function verify(token: string, secretOrPublicKey: Secret, options?: VerifyOptions): DecodedJwtToken;
}