DEV Community

Property 'id' does not exist on type 'string | JwtPayload' [duplicate]

Emmanuel Kelechi Igwesi on January 19, 2023

I just want to get user by id but I got an error that states id does not exist on type string , my code is shown below: import asyncHandler from...
Collapse
 
moscode profile image
Moscode

I had this same issue about a few minutes ago.

The error occurs because the type of the "verified" variable is not specified; Hence it is 'string | JwtPayload'.

So I solved the error by specifying the type to be "JwtPayload". I.e

const verified = jwt.verify(token, configs.JWT_SECRET)
Enter fullscreen mode Exit fullscreen mode

became

const verified = jwt.verify(token, configs.JWT_SECRET) as JwtPayload
Enter fullscreen mode Exit fullscreen mode

With Bug

Image description

Fixed bug

Image description

Collapse
 
dchief profile image
Emmanuel Kelechi Igwesi

I added id: string; to the jwtpayload but still got same error, code shown below:

export interface JwtPayload {
    [key: string]: any;
    id: string;
    iss?: string | undefined;
    sub?: string | undefined;
    aud?: string | string[] | undefined;
    exp?: number | undefined;
    nbf?: number | undefined;
    iat?: number | undefined;
    jti?: string | undefined;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mafelo profile image
Kennedy Mafelo
import jwt, { type JwtPayload, type Secret } from "jsonwebtoken";

interface CustomJwtPayload extends JwtPayload {
    _id: string;
}

 const decoded = jwt.verify(
        token,
        process.env.JWT_SECRET as Secret
    ) as CustomJwtPayload;
Enter fullscreen mode Exit fullscreen mode