如何为Piped ES6函数生成JSDoc


10

我有一个ES6样式的函数,该函数是使用组成的asyncPipe

import { getItemAsync } from 'expo-secure-store';

const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x);

const getToken = () => getItemAsync('token');

const liftedGetToken = async ({ ...rest }) => ({
  token: await getToken(),
  ...rest,
});

const liftedFetch = ({ body, route, token, method = 'GET' } = {}) =>
  fetch(route, {
    ...(body && { body: JSON.stringify(body) }),
    headers: {
      'Content-Type': 'application/json',
      ...(token && { Authorization: `Bearer ${token}` }),
    },
    method,
  });

const json = res => res.json();

/**
 * @method
 * @param {Object} fetchSettings the settings for the fetch request
 * @param {Object} fetchSettings.body the body of the request
 * @param {string} fetchSettings.route the URL of the request
 * @param {string} fetchSettings.method the method of the request
 * @param {string} fetchSettings.token should only be used for testing and unauthenticated requests
 */
const request = asyncPipe(liftedGetToken, liftedFetch, json);

如您所见,我尝试向其中添加JSDoc描述。但是,当我在任何地方使用它时,我的编辑器VSCode都不会建议其参数。如何使用JSDoc声明这些类型的函数?以及如何使此函数与Intellisense一起使用,需要获得参数?


Answers:


1

VSCode在后台使用TypeScript引擎,该引擎不擅长从函数组合中推断类型,并且如您所见,无法将无点组合识别为函数声明。

如果需要类型提示,则可以通过在其周围包装一个指向函数来指定组合函数的参数。

我会这样写-注意:默认值使JSDoc不再需要类型提示,但是无论如何,您可能都希望保留JSDoc来进行描述。还应确保由默认值回退导致的故障会产生足够的错误消息。

/**
  * http request with JSON parsing and token management.
  * @param {Object} fetchSettings the settings for the fetch request
  * @param {Object} fetchSettings.body the body of the request
  * @param {string} fetchSettings.route the URL of the request
  * @param {string} fetchSettings.method the method of the request
  * @param {string} fetchSettings.token should only be used for testing and unauthenticated requests
  */
const request = ({
  body = {},
  route = '',
  method = 'GET',
  token = ''
}) => asyncPipe(liftedGetToken, liftedFetch, json)({
  body, route, method, token
});

6

VSCode将尝试在其中显示匿名函数的注释asyncPipe。如果在其中添加JSDoc注释,则可以看到以下行为:

const asyncPipe = (...fns) =>
  /**
   * My asyncPipe description
   * @param {Object} x Any object
   */
  x => fns.reduce(async (y, f) => f(await y), x);

const request = asyncPipe(liftedGetToken, liftedFetch, json);

例

不幸的是,JSDoc中无法像您尝试的那样覆盖匿名函数的文档。但是,您可以像这样将意图强加给VSCode,请注意,这会引入额外的函数调用:

const doRequest = asyncPipe(liftedGetToken, liftedFetch, json);

/**
 * @method
 * @param {Object} fetchSettings the settings for the fetch request
 * @param {Object} fetchSettings.body the body of the request
 * @param {string} fetchSettings.route the URL of the request
 * @param {string} fetchSettings.method the method of the request
 * @param {string} fetchSettings.token should only be used for testing and unauthenticated requests
 */
const request = fetchSettings => doRequest(fetchSettings);

解决方案示例

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.