带有@lends(或类似)的jsdoc类无法被intellisense识别
我目前正在开发一个项目,该项目是在 es6 类出现之前编写的,因此对其所有类使用类创建函数。
我正在使用 VSCode,并且认为使用一些 IntelliSense 会很好,以便实例显示它们的方法、参数和返回类型等,因此我使用 JSDoc 记录它们。
我很快发现 JSDoc 允许记录 es6 之前的类。 这些文档几乎包含了我的问题,请参阅 jsdoc 标签文档:@lends。 给定的示例显示以下内容:
/** @class */
var Person = makeClass(
/** @lends Person.prototype */
{
/**
* Create a `Person` instance.
* @param {string} name - The person's name.
*/
initialize: function(name) {
this.name = name;
},
/**
* Say something.
* @param {string} message - The message to say.
* @returns {string} The complete message.
*/
say: function(message) {
return this.name + " says: " + message;
}
}
);
然而,当我将此示例复制粘贴到空的 javascript 文件中并在下面添加另一行时,例如
var p = new Person("");
p.say("")
我的编辑器 VSCode,对 p.say 没有智能提示提示,参数消息(不是t 根本无法识别),p 和 var Person 显示为 type: any 。我本来期望 p 具有 Person 类型,并且智能感知可以通过其方法识别该类。 为什么不是这样,我做错了什么?
我已经尝试添加 @type Person 或其他一些标签,但我想避免使用像 .d.ts 这样的单独文件,仅用于类型提示或将“文档”和代码分开太多。带有 jsdoc 的函数 foobar(foo, bar){ ... } 的文档可以工作并显示类型提示,但在变量赋值后保留类型信息似乎是一个问题。
(显然:将整个项目更改为更新的 javascript 版本甚至 typescript 都不是一个选择。)
提前感谢您的帮助!
I am currently working on a project that was written before es6 classes were a thing and therefor uses a class creation function for all of its classes.
I am using VSCode and thought, it would be nice to use some IntelliSense, so that instances show their methods, the parameters and return types, and so on, therefor I documented them using JSDoc.
I quickly found out that JSDoc allows Documentation of pre-es6-classes.
The docs nearly include my problem, see jsdoc tag documentation: @lends.
The given example shows the following:
/** @class */
var Person = makeClass(
/** @lends Person.prototype */
{
/**
* Create a `Person` instance.
* @param {string} name - The person's name.
*/
initialize: function(name) {
this.name = name;
},
/**
* Say something.
* @param {string} message - The message to say.
* @returns {string} The complete message.
*/
say: function(message) {
return this.name + " says: " + message;
}
}
);
Yet, when I copy-paste this example into an empty javascript file and add another line below, like
var p = new Person("");
p.say("")
My editor, VSCode, does no intellisense-hinting for p.say, the parameter message (which isn't recognized at all) and p and var Person is shown as type: any . I would have expected that p would have the type Person and that intellisense recognizes the class with its methods.
Why isn't this the case, what am I doing wrong?
I already tried to add @type Person or some other tags, but I would like to avoid having a separate file like .d.ts just for type hinting or separating "documentation" and code to much. The documentation of a function foobar(foo, bar){ ... } with jsdoc works and type hinting is shown, but keeping the type information after variable assignments seem to be a problem.
(Obviously: Changing the entire project to a newer javascript version or even typescript is not an option.)
Thanks for any help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于发现这个问题的人:
罪魁祸首似乎是 Visual Studio Code。
使用 IntelliJ 的 Webstorm IDE,可以使用
@lends
表示法,并显示名称完成(包括类型提示)。亲爱的读者,如果您遇到同样的问题并且正在使用 VSCode,请测试另一个 IDE,它实际上可能有效...
To whoever finds this question:
The main culprit seems to have been Visual Studio Code.
Using IntelliJ's Webstorm IDE, the
@lends
notation works and name completion including type hinting are shown.If you, dear reader, have the same problem and are using VSCode, test another IDE, it may actually work...