0

I am using Firebase Cloud Functions to execute server-side code when called by client via React app. I have many different functions, each of which takes different input parameters, and return a data object with various attributes.

I would like to create a hook called useFirebaseFunctions.ts using which I can make the request, and in doing so, see all the input parameters that function requires, and the expected output results.

Within my hook, I've written the following function:

async function generateReport({ organisationId, projectId }: {
    organisationId: string;
    projectId: string;
}): Promise<{
    data: {
        id: string;
        fileName: string;
        numOfRows: number;
        createdOn: number;
    };
}> {
    const request = httpsCallable(functions, 'adminFunctions-generateReport');
    return request({ organisationId, projectId });
}

For the adminFunctions-generateReport function, the parameters are:

Input Output
organisationId: string id: string
projectId: string fileName: string
numOfRows: number
createdOn: number

Issue

However, TypeScript displays an error on the return request({...)}; line:

Type 'HttpsCallableResult' is not assignable to type '{ data: { id: string; fileName: string; numOfRows: number; createdOn: number; }; }'. Types of property 'data' are incompatible. Type '{}' is missing the following properties from type '{ id: string; fileName: string; numOfRows: number; createdOn: number; }': id, fileName, numOfRows, createdOn

How can I properly type the function to get expected behaviour without errors?

1 Answer 1

1

I found the solution. I re-declared the function as:

async function generateReport({ ...params }: {
    organisationId: string;
    projectId: string;
}) {
    interface ResInterface {
        id: string;
        fileName: string;
        numOfRows: number;
        createdOn: number;
    };

    const response = httpsCallable<any, ResInterface>(functions, 'adminFunctions-generateReport');
    return (await response({ ...params })).data;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.