Skip to content

Intellisense for JS object's this not working in function call #43930

Closed

Description

  • VSCode Version: 1.20.1
  • OS Version: macOS 10.13.3
  • 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):

const myObject = {};

const addObjectValue = (key, value) => {
  myObject[key] = value;
};

addObjectValue("hello", {
  a: "world",
  log() {
    // Here, this is not autocompleted with this.a
    console.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.

Activity

  1. Floriferous commented on Feb 18, 2018

    @Floriferous
    Author

    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:

    const myObject = {};
    
    const addObjectValue = (key, value) => {
      myObject[key] = value;
    };
    
    addObjectValue("hello", {
      a: "world",
      log() {
        // Here, this is not autocompleted with this.a
        console.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?

  2. added
    bugIssue identified by VS Code Team member as probable bug
    upstreamIssue identified as 'upstream' component related (exists outside of VS Code)
    javascriptJavaScript support issues
    on Feb 20, 2018
  3. mjbvz commented on Feb 20, 2018

    @mjbvz
    Contributor

    Thanks. Opened microsoft/TypeScript#22058 to track this upstream

  4. locked and limited conversation to collaborators on Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugIssue identified by VS Code Team member as probable bugjavascriptJavaScript support issuesupstreamIssue identified as 'upstream' component related (exists outside of VS Code)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions