I am encountering an issue with my object named endpoints
that contains various methods:
const endpoints = {
async getProfilePhoto(photoFile: File) {
return await updateProfilePhotoTask.perform(photoFile);
},
};
To access these methods, I am using a function that takes a string argument to construct a template literal and then accesses the method through bracket notation:
export const useApiTask = (
endpointName: string,
) => {
//const apiActionName = 'getProfilePhoto'; // Strings work fine
const apiActionName = `get${endpointName}`; // However, template literals cause a TypeScript error
const endpointHandler = endpoints[apiActionName]; // This is where the TypeScript error occurs
}
The use of template literals triggers a TypeScript error on
endpointHandler = endpoints[apiActionName]
:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ getProfilePhoto(photoFile: File): Promise<string | undefined>; }'.
No index signature with a parameter of type 'string' was found on type '{ getProfilePhoto(photoFile: File): Promise<string | undefined>; }'. ts(7053)
Do you have any insight into what might be causing this issue?