Javascript 如何为'pipe`d ES6函数生成JSDoc
我有一个ES6风格的函数,它是使用函数组合和Javascript 如何为'pipe`d ES6函数生成JSDoc,javascript,ecmascript-6,functional-programming,intellisense,jsdoc,Javascript,Ecmascript 6,Functional Programming,Intellisense,Jsdoc,我有一个ES6风格的函数,它是使用函数组合和asyncPipe定义的 从“expo secure store”导入{getItemAsync}; const asyncPipe=(…fns)=>x=>fns.reduce(异步(y,f)=>f(等待y),x); const getToken=()=>getItemAsync('token'); const-liftedGetToken=async({…rest})=>({ 令牌:等待getToken(), 休息 }); const liftedF
asyncPipe
定义的
从“expo secure store”导入{getItemAsync};
const asyncPipe=(…fns)=>x=>fns.reduce(异步(y,f)=>f(等待y),x);
const getToken=()=>getItemAsync('token');
const-liftedGetToken=async({…rest})=>({
令牌:等待getToken(),
休息
});
const liftedFetch=({body,route,token,method='GET'}={})=>
取回(路线){
…(body&&{body:JSON.stringify(body)}),
标题:{
“内容类型”:“应用程序/json”,
…(令牌和&{授权:`Bearer${token}`),
},
方法,,
});
const json=res=>res.json();
/**
*@方法
*@param{Object}fetchSettings获取请求的设置
*@param{Object}fetchSettings.body请求的主体
*@param{string}fetchSettings.route请求的URL
*@param{string}fetchSettings.method请求的方法
*@param{string}fetchSettings.token应仅用于测试和未经身份验证的请求
*/
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);
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
});
相关:和