简体   繁体   English

如何在 TypeScript 中使用 JSDoc 记录“提供者模式”功能?

[英]How do I document "provider-pattern" functions with JSDoc in TypeScript?

Taking a minimal example, here is what I'm calling a "provider-pattern" function (please correct me if there's another name here).举一个最小的例子,这就是我所说的“提供者模式”function(如果这里有另一个名字,请纠正我)。

export const callEndpointProvider = (endpointToCall: string) => 
  async (accessToken: string): Promise<null> => { 
    return null 
  }

I'd like to document this function with JSDoc in such a way that the returned function is documented, not necessarily the provider itself.我想用 JSDoc 记录这个 function 以记录返回的 function,不一定是提供程序本身。

I've looked around on the internet and SO and can't seem to find anything to point me in the right direction, so for now I've sufficed with documenting the provider itself as though it was the provided function.我在互联网上环顾四周,似乎找不到任何可以为我指明正确方向的东西,所以现在我已经足够记录提供者本身,就好像它是提供的 function 一样。

The issue with this approach being, I don't get the nice IDE tooltips on my provided function after initializing the provider.这种方法的问题是,在初始化提供程序后,我在提供的 function 上没有得到很好的 IDE 工具提示。

JSDoc example (currently what I have): JSDoc 示例(目前我有):

/**
* A function that returns null
* @async
* @return {null} - A null value
*/

I'm also open to alternatives to JSDoc if there's something that fits this usecase better, as we use this pattern a lot.如果有更适合这个用例的东西,我也愿意接受 JSDoc 的替代品,因为我们经常使用这种模式。

You can do that by putting the JSDoc inside the function, annotating the returned function:您可以通过将 JSDoc 放在 function 中,注释返回的 function 来做到这一点:

export const callEndpointProvider = (endpointToCall: string) => {
    /**
     * A function that returns null.
     *
     * @param   accessToken The access token to use.
     */
    return async (accessToken: string): Promise<null> => {
        return null;
    };
};

(Since you're using TypeScript, the types don't have to be in the JSDoc.) (由于您使用的是 TypeScript,因此类型不必在 JSDoc 中。)

Here's an example of using the returned function and the information associated with it (in this case, by VS Code):下面是使用返回的 function 及其相关信息的示例(在本例中,通过 VS Code):

VS Code 编辑器显示使用上述函数返回的函数,以及带有函数文档的信息面板。

That's in addition to documenting the function itself, of course:当然,除了记录 function 本身之外:

/**
 * A function returning a function to call an endpoint.
 *
 * @param   endpointToCall  The endpoint the returned function will call.
 * @returns The function.
 */
export const callEndpointProvider = (endpointToCall: string) => {
    /**
     * A function that returns null.
     *
     * @param   accessToken The access token to use.
     */
    return async (accessToken: string): Promise<null> => {
        return null;
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM