【问题标题】:Javascript Intellisense no longer working for object properties assigned after initial object creation in Visual Studio 2022Javascript Intellisense 不再适用于在 Visual Studio 2022 中创建初始对象后分配的对象属性
【发布时间】:2021-12-22 02:17:50
【问题描述】:

与 VS2019 相比,在 VS2022 中,Javascript Intellisense 服务似乎进行了一些重大修订。

Javascript Intellisense 似乎不再识别在初始创建上下文之外分配的对象属性。

var r = { a: 1, b: 2 };
r.c = 3;
//"r.a" and "r.b" will here be identified by Intellisense, but not "r.c".

当 AngularJs 项目中存在作用域和依赖注入对象时,这是一些非常令人沮丧的行为,因为这些不再提供智能感知自动完成或使用“转到定义”的导航。

这在 VS2019 中 没有 JSDoc 标头之前效果很好。

Visual Studio 2019

a,b,c,d,e 都可以在这里找到。

Visual Studio 2022

此处仅提供 a,b,d

是否有任何已知的设置或包来改变/纠正这种新行为?

【问题讨论】:

    标签: javascript intellisense javascript-intellisense visual-studio-2022


    【解决方案1】:

    发生这种情况是因为在 javascript 中您没有类型声明,并且由于您只声明了两个属性,因此假设您的对象只包含那些。 您可以通过添加 cmets 来避免这种情况,代码/逻辑不会发生任何变化,但智能感知会识别它们

    /** @type { {a: number, b: number, c?: number} } */
    const obj = { a: 1, b: 2 };
    
    // Now you can access every property your object has (not only the declared) and intellisense will recognize those declared
    

    你可以用同样的方式声明:

    /** @type {string} */
    const num = 12;
    
    // Now intellisense will recognize it as a string even if it's a number
    // and because in JS it's not an error to assign a number or a string to
    // a variable it will throw an error at runtime if you try thing like
    // num.trim();
    

    确保您要声明的内容

    【讨论】:

    • 感谢您的回答。这里的主要问题是我的示例以前在没有 JsDoc 标头的情况下在 VS2019 中工作。我目前正在从事的项目是一个主要的 AngularJs 项目,当对象被注入依赖项时,JsDoc 标头不会被传递。因此注入的工厂或控制器将不再被识别为 VS 中所述对象的实例。无论如何,由于任务的艰巨性,将 JsDoc 标头添加到整个项目的每个部分都不是一个可行的选择。
    • 好吧,我误解了你的问题,在角度你也使用打字稿,所以你不需要 cmets。这很奇怪,我不知道它可能是什么。我赞成您的问题,以便其他人可以看到它
    • 不用担心。使用 JSDoc 是非常好的做法,应该在任何适用的地方使用。尽管在这种情况下,实施起来并不现实。我还想指出,Angular 和 AngularJs 是两个完全不同的框架。正如您所说,AngularJs 不使用 TypeScript,但 Angular 使用了。 AngularJs 实际上是 Angular 1.0,可以看作是 Angular 2+ 的先驱。由于 Angular 2 是对整个概念的彻底改造,因此它们的工作方式存在几个主要差异。
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2013-02-09
    • 2016-03-20
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多