Currently, I am developing a custom `useProject` hook in TypeScript that is utilizing a function defined in `useProjectFunctions.ts`.
While working on the `useProject` hook, I encountered a TypeScript error indicating type mismatch, although I am unable to identify any discrepancies.
useProjectFunctions.d.ts
export declare type GetImageUploadToken = ({ ...params }: {
organisationId: string,
projectId: string,
imageName: string,
isAdminRequest?: boolean,
}) => {
status: 'success';
tokenId: string;
}
useProjectFunctions.ts
export function useProjectFunctions() {
const { functions } = useFirebase();
async function getImageUploadToken({ ...params }: Parameters<GetImageUploadToken>[0]) {
const response = httpsCallable<any, ReturnType<GetImageUploadToken>>(functions, ProjectFunctions.GET_IMAGE_UPLOAD_TOKEN);
return (await response({ ...params })).data;
};
return {
getImageUploadToken
};
}
useProject.ts
export function useProject() {
const { getImageUploadToken } = useProjectFunctions();
return {
addImage: () => addImage({ getImageUploadToken })
};
}
async function addImage({ getImageUploadToken }: {
getImageUploadToken: GetImageUploadToken;
}) {
const tokenCallResponse = getImageUploadToken({
organisationId: 'orgId1',
projectId: 'projId1',
imageName: 'fileName',
isAdminRequest: false
});
}
When running useProject.ts
, TypeScript throws an error at the
addImage: () => addImage({ getImageUploadToken })
line:
The assigned type '({ ...params }: { organisationId: string; projectId: string; imageName: string; isAdminRequest?: boolean; }) => Promise<{ status: "success"; tokenId: string; }>' does not match the expected type 'GetImageUploadToken'.
The returned type 'Promise<{ status: "success"; tokenId: string; }>' is missing properties 'status' and 'tokenId' from the expected type '{ status: "success"; tokenId: string; }'.
This discrepancy originates from the property 'getImageUploadToken' declared in '{ project: PrivateProjectInterface; setProject: Dispatch<SetStateAction>; file: File; admin?: boolean; getImageUploadToken: GetImageUploadToken; storage: FirebaseStorage; }'
Can someone explain the true meaning of this error and suggest a solution?