我有一个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一起使用?
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);