带有注释参数属性的Typescript jsdoc
我试图注释一个对象的属性,它是一个函数的参数 具体来说,我希望在将鼠标悬停在函数定义上时,vscode中会显示带有注释参数属性的Typescript jsdoc,typescript,visual-studio-code,jsdoc,Typescript,Visual Studio Code,Jsdoc,我试图注释一个对象的属性,它是一个函数的参数 具体来说,我希望在将鼠标悬停在函数定义上时,vscode中会显示options.length解释 /** *记忆一个函数 *@param{(…fnArgs:T[])=>U}fn用于记忆的函数 *@param{Object}options记忆化选项 *@param{number}options.max LRU缓存的最大大小 *@param{number | undefined}options.length将缓存响应 *根据此值,由前N个参数指定。尾随
options.length
解释
/**
*记忆一个函数
*@param{(…fnArgs:T[])=>U}fn用于记忆的函数
*@param{Object}options记忆化选项
*@param{number}options.max LRU缓存的最大大小
*@param{number | undefined}options.length将缓存响应
*根据此值,由前N个参数指定。尾随args仍将为空
*传递给基础函数,但在记忆过程中将被忽略。
*/
导出常量备忘录=(
fn:(…fnArgs:T[])=>U,
{max,length}:{max:number;length?:number}
) => {
常量缓存:T[][]=[]
常量缓存值:U[]=[]
常量get=(args:T[]):U |未定义=>{
const index=cachedArgs.findIndex(x=>argsAreEqual(x,args,length))
如果(索引==-1)返回
使用(索引)
返回CachedValue[索引]
}
常量集=(参数:T[],值:U)=>{
cachedArgs.push(args)
cachedValues.push(值)
}
const onUsed=(索引:number)=>{
moveToEnd(索引、缓存线)
moveToEnd(索引、缓存值)
}
常量prune=()=>{
如果(cachedArgs.length>=最大值){
cachedArgs.shift()
cachedValues.shift()
}
}
返回(…参数:T[])=>{
let value=get(args)
if(value)返回值
李子
值=fn(…参数)
设置(参数、值)
返回值
}
}
当鼠标悬停在类型签名上时,我得到以下结果
@param fn — The function to memoize
@param options — Memoization options
我已经尽了最大努力,但它没有显示对选项.length的解释
我想让它显示options.length
参数如何工作的解释。我该怎么做
还有一个问题,不知道如何使泛型与jsdoc一起工作。。。非常感谢您对@模板的帮助 此行为似乎是一个已知的错误,因此,非结构化参数不会显示其JSDoc注释:。我真的不知道为什么,但是根据,给一个@param
一种类型的对象
相当于给它一种类型的any
,你可以通过使用不同的类型来获得你想要的行为。在下面我将对象
更改为{}
:
/**
* @param {(...fnArgs: T[]) => U} fn
* @param {{}} options Memoization options
* @param {number} options.max The max size of the LRU cache
* @param {number | undefined} options.length The response will be cached
* by the first N args, based on this value. Trailing args will still be
* passed to the underlying function but will be ignored during memoization.
*/
export const memo = <T, U>(
fn: (...fnArgs: T[]) => U,
{ max, length }: { max: number; length?: number }
) => { }
请注意,这似乎只适用于TypeScript代码(如.ts
或.tsx
),而不适用于JavaScript代码。如果在JavaScript中执行此操作,编译器将抱怨除对象
或对象
之外的任何内容
至于你的奖金问题,我认为它应该像在JavaScript中一样在TypeScript中工作:
/**
* @template T the arg type of fn
* @template U the return type of fn
* @param {(...fnArgs: T[]) => U} fn
这让我明白
/* IntelliSense shows:
const memo: <T, U>(fn: (...fnArgs: T[]) => U, { max, length }: {
max: number;
length?: number | undefined;
}) => void
@template — T the arg type of fn
@template — U the return type of fn
@param fn
*/
/*智能感知显示:
常数备忘录:(fn:(…fnArgs:T[])=>U,{max,length}:{
最大:数量;
长度?:数字|未定义;
})=>无效
@模板-T fn的arg类型
@模板-U fn的返回类型
@参数fn
*/
我从未使用过此功能,但也许您可以使用@prop
或@property
来代替,比如?啊,这显然是一个已知的错误:,其中列出了可能的解决方法。或者也许TS代码不支持它,就像在解决方案中对您有用一样?我不知道为什么Object
会阻止@param
工作,但相关问题似乎表明会发生这种情况。太棒了!如果你写一个答案,我会尽快批准。否则,我很乐意自己承担责任并写下答案!呵呵
/* IntelliSense shows:
const memo: <T, U>(fn: (...fnArgs: T[]) => U, { max, length }: {
max: number;
length?: number | undefined;
}) => void
@template — T the arg type of fn
@template — U the return type of fn
@param fn
*/