Reputation: 808
I have written a wrapper function memoize
that caches the results of a function by its arguments. I would like the type hint for the wrapped function to show the original docs.
Here is what the type hint currently shows:
And here is how I have declared my Memoized
type:
/**
* Functions that match this type can be memoized.
*/
type Memoizable = (...args: any[]) => any;
/**
* The memoized function.
*/
interface Memoized<T extends Memoizable> {
/**
* Memoized function that caches its results by a key built from its arguments.
*/
(...args: Parameters<T>): ReturnType<T>;
/**
* The cache from which memoized items are retrieved.
*/
readonly cache: Map<string, ReturnType<T>>;
}
So instead if the documentation for Memoized
, I want to show the documentation for my function test
. My guess is that I need to declare Memoized
differently. What am I missing?
View the full example on TS Playground
Upvotes: 0
Views: 827
Reputation: 808
Turns out the solution was to declare Memoized
as follows:
/**
* Memoized function that exposes its cache.
*/
type Memoized<T extends Memoizable> = T & Memo<T>
/**
* A memoized function exposes its cache as a read-only attribute.
*/
interface Memo<T extends Memoizable> {
/**
* The cache from which memoized items are retrieved.
*/
readonly cache: Map<string, ReturnType<T>>;
}
Now the type hint looks like this:
View the full example on TS Playground
Upvotes: 1