gpt4 book ai didi

Javascript Intellisense 不再适用于在 Visual Studio 2022 中创建初始对象后分配的对象属性

转载 作者:行者123 更新时间:2023-12-05 00:33:37 26 4
gpt4 key购买 nike

与 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 项目中存在作用域和依赖注入(inject)对象时,这是一些非常令人沮丧的行为,因为它们不再提供智能感知自动完成或使用“转到定义”的导航。
这以前效果很好 没有 VS2019 中的 JSDoc header 。
Visual Studio 2019 a , b , c , d , e都可以在这里找到。
Intellisense correctly working in VS2019
Visual Studio 2022
仅限 a , b , d在这里可用。
VS2022 no longer recognizes properties assigned outside of the creation context
是否有任何已知的设置或包来改变/纠正这种新行为?

最佳答案

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

/** @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
image
以同样的方式你可以声明:
/** @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();
确保您要声明的内容

关于Javascript Intellisense 不再适用于在 Visual Studio 2022 中创建初始对象后分配的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69897071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com