[英]Inherit JSDoc from generic parameter in TypeScript
I have written a wrapper function memoize
that caches the results of a function by its arguments.我编写了一个包装函数
memoize
,它通过参数缓存函数的结果。 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:这是我如何声明我的
Memoized
类型:
/**
* 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
.因此,如果是
Memoized
的文档,我想显示我的功能test
的文档。 My guess is that I need to declare Memoized
differently.我的猜测是我需要以不同的方式声明
Memoized
。 What am I missing?我错过了什么?
View the full example on TS Playground 在 TS Playground 上查看完整示例
Turns out the solution was to declare Memoized
as follows:原来的解决方案是声明
Memoized
如下:
/**
* 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 在 TS Playground 上查看完整示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.