Here is an example of a method:
export default class ApiService {
static makeApiCall = (
url: string,
normalizeCallback: (data: ResponseData) => ResponseData | null,
callback: (data: any) => any
): Promise<void> => (
ApiClient.get(url)
.then(response => {
callback(normalizeCallback(response.data));
})
.catch(error => {
console.error(`ApiClient ${url}`, error);
})
)
static getArticles = (callback: (articles: Article[]) => void): Promise<void> => (
ApiService.makeApiCall(
'articles',
ApiNormalizer.normalizeArticles,
callback
)
)
}
The line causing a warning in TypeScript is callback: (data: any) => any
warning Unexpected any. Specify a different type
To provide more context, here is how the method getArticles
is being called
export const fetchArticles = (): ThunkAction<Promise<void>,{},{},AnyAction> => {
return async (dispatch: ThunkDispatch<{}, {}, AnyAction>): Promise<void> => {
dispatch(() => ({ type: FETCH_ARTICLES }));
// HERE: getArticles
return ApiService.getArticles(articles => dispatch(loadArticles(articles)));
};
};
How do I properly type the callback function in this scenario?