You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Intellisense for JS object's this not working in function call #43930
Does this issue occur when all extensions are disabled?: Yes
this does not properly autocomplete when an object literal is passed as a parameter to a function.
Here's a reproduction (there's certainly a simpler one):
constmyObject={};constaddObjectValue=(key,value)=>{myObject[key]=value;};addObjectValue("hello",{a: "world",log(){// Here, this is not autocompleted with this.aconsole.log(this.a);}});myObject.hello.log();// Logs 'world' properly
It may be that this is by design for some obscure JS reasons, in which case I apologise for this issue.
Turns out my this became undefined when I ran the code (in my more complicated setup). And it's indeed because of a JS quirk.
Here's how it failed:
constmyObject={};constaddObjectValue=(key,value)=>{myObject[key]=value;};addObjectValue("hello",{a: "world",log(){// Here, this is not autocompleted with this.aconsole.log(this.a);}});const{ log }=myObject.hello;hello.log();// ERROR: Cannot read property 'a' of undefined
When you destructure log out of myObject.hello, it's context is undefined. So maybe this issue is a feature rather than a bug trying to prevent developers from making mistakes?
Activity
Floriferous commentedon Feb 18, 2018
Turns out my
thisbecame undefined when I ran the code (in my more complicated setup). And it's indeed because of a JS quirk.Here's how it failed:
When you destructure
logout ofmyObject.hello, it's context isundefined. So maybe this issue is a feature rather than a bug trying to prevent developers from making mistakes?mjbvz commentedon Feb 20, 2018
Thanks. Opened microsoft/TypeScript#22058 to track this upstream