I want to wrap my service methods in a cachify method that checks the cache before querying the database. However, I am unable to preserve the type declaration of the wrapped function.
The wrapping cachify function looks like this:
// cache.ts
const cachify = async <T>(fn, args): Promise<T> => {
const key = constructHashKey(args)
const cachedEntry = get(key)
if (cachedEntry) {
return cachedEntry
} else {
const entry = await fn(...args)
put(key, entry)
return entry
}
}
Here is an example of the usage of the wrapping function:
// userService.ts
const getUserProfilePhotoUrl = async (id: string, size: string): Promise<string> => {
return cachify<string>(fetchPhotoUrl, [
id,
size
])
}
The fetchPhotoUrl function has the signature (id: string, size: string): Promise<string>.
However, if I add some arbitrary argument to the array [id, size], I do not get any type errors. How do I make Typescript aware of this?