skip to Main Content

I am trying to declare the type of return data in a function. Here’s the function:

export const getCurrentUserData = async (): User => {
    const value = await storage.getItem('currentSite');
    if (value === null) {
        return '';
    } else {
        const getuserData = JSON.parse(value)
        const userData = {
            email: getuserData.userData.email,
            id: getuserData.userData.id,
            name: getuserData.userData.name,
            userKey: getuserData.userData.userKey,
            userRole: getuserData.userData.userRole,
            photo: getuserData.userData.photo,
        }
        return userData;
    }
}

Here’s my data type:

export type User = {
    email: string,
    id: string,
    name: string,
    userKey: string,
    userRole: string,
    photo: string,
}

Am i doing these the right way? Because it shows the error: 'Type 'User' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor '

2

Answers


  1. You need to include Promise in the signature like so:

    export const getCurrentUserData = async (): Promise<User> => {
    ...
    }
    
    Login or Signup to reply.
  2. Try this code

    export const getCurrentUserData = async (): Promise<User> => {
    const value = await storage.getItem('currentSite');
    if (value === null) {
        throw new Error('Current site data not found');
    } else {
        const getuserData = JSON.parse(value);
        const userData: User = {
            email: getuserData.userData.email,
            id: getuserData.userData.id,
            name: getuserData.userData.name,
            userKey: getuserData.userData.userKey,
            userRole: getuserData.userData.userRole,
            photo: getuserData.userData.photo,
        };
        return userData;
    }
    

    };

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search