从 TypeScript 中的泛型参数继承 JSDoc

Inherit JSDoc from generic parameter in TypeScript

我写了一个包装函数 memoize,它通过参数缓存函数的结果。我想要包装函数的类型提示以显示原始文档。

这是类型提示当前显示的内容:

这是我声明我的 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>>;
}

因此,如果 Memoized 的文档,我想显示我的函数 test 的文档。我的猜测是我需要以不同的方式声明 Memoized 。我错过了什么?

View the full example on TS Playground

原来解决方案是声明 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>>;
}

现在类型提示如下所示:

View the full example on TS Playground