Skip to content

JavaScript 'this.property' not recognised when assigned to with '=' #51359

@asgoode

Description

@asgoode

VS Code Version: 1.73.0
TypeScript Version: 4.8.4
OS Version: Windows_NT x64 10.0.22621 (Windows 11)
Does this issue occur when all extensions are disabled?: Yes

Description

Normally, when a variable/property symbol is selected, that symbol and all other references to it in the same scope are highlighted.

In cases where an object's property has a value assigned to it with the = operator within a method, the property's symbol seems to lose its 'identity' and is no longer highlighted when other instances of that symbol in the same scope are selected. More importantly, related features such as Rename and Find all references also fail to recognise the symbol.

This issue describes the behaviour in VS Code. It was also tried in TS Playground with the same TS version and TS Config / Lang set to JavaScript, with slightly different results as indicated below.

Steps to reproduce

These examples can be reproduced with or without all extensions disabled.

Example 1 - one object

This is reproducible in VS Code. It works ok in TS Playground.

Paste this code into an empty JavaScript editor:

const o1 = {
  v: 1,
  returnV() { return this.v },
  incrementV() { this.v += 1 },
  assignV() { this.v = 2 },
};
o1.v = 2;

Select the v on line 2. All instances of 'v' are highlighted as expected, and hovering over any of those instances shows the following:

(property) v: number

Now, comment line 2:

  // v: 1,

then uncomment it again (or just undo):

  v: 1,

Select the v on line 2 again. Now, the instance of v in the assignV method is not highlighted, and hovering over it shows undefined as another possible type:

(property) v: number | undefined

Similarly, selecting the v in the assignV method does not highlight the other instances.

This behaviour seems to only happen where a value is assigned to this.v with the = operator. Note that o2.v, with the same assignment, is not affected. Also, selecting any this symbol still highlights all other this symbols.

A small edit in the body of the assignV method, like adding a space, seems to re-evaluate the symbol, and highlighting/hovering again works as expected.

Example 2 - using this of another object

This is reproducible in both VS Code and TS Playground.

This case is similar to Example 1, except this in the methods refers to another object. (This scenario is used in a real project.)

Paste this code into an empty Javascript editor:

const o2 = {
  v: 1,
};
const o3 = {
  /** @this o2 */
  returnV() { return this.v },
  /** @this o2 */
  incrementV() { this.v += 1 },
  /** @this o2 */
  assignV() { this.v = 2 },
};
o2.v = 2;

(The @this o2 JSDoc before each method tells those methods to use o2 as their this object. An alternative 'function type' JSDoc is @type {(this: typeof o2) => *}, but the behaviour is the same.)

Select the v in o2. All instances of v are highlighted except for the instance in the assignV method. Unlike Example 1, there doesn't seem to be a way to re-evaluate that symbol – it remains unrecognised as an instance of v.


The problem seems to be unique to the assignment of a value to a property of this with the = operator within an object method. It doesn't happen with other operators that change its value, like += or ++, or where this.property is only read, or when assigning with = outside of a method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions