简体   繁体   中英

Javascript Intellisense no longer working for object properties assigned after initial object creation in Visual Studio 2022

In VS2022 there seems to have been some major revision in the Javascript Intellisense Service in comparison to VS2019.

Javascript Intellisense no longer seems to recognize object properties assigned outside of the initial creation context.

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

This is some highly frustrating behaviour when there are scopes and dependency injected objects like in an AngularJs project since these no longer offer intellisense autocomplete or navigation using "go to definition".

This has previously worked very well without JSDoc headers in VS2019.

Visual Studio 2019

a , b , c , d , e are all available here.

Intellisense 在 VS2019 中正常工作

Visual Studio 2022

Only a , b , d are available here.

VS2022 不再识别在创建上下文之外分配的属性

Is there any known setting or package to alter/correct this new behaviour?

This happens because in javascript you don't have type declarations and since you're delaring just two properties it supposes taht your object contains only those. You can avoid this by adding comments, no changes in the code/logic but intellisense will recognize them

/** @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

图片

In the same way you can declare:

/** @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();

Make sure of what you're declaring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM