Skip to content

The typescript language service does not interpret "this" in object literals correctly. #61222

Closed as not planned
@snuup

Description

@snuup

Does this issue occur when all extensions are disabled?: Yes/No

  • VS Code Version: 1.97.2
  • OS Version: x64 10.0.19045

Steps to Reproduce:

Type the code

let m = { a: 3, f() { return this.a } }

into a file and store it with "js" and then with "ts" extension.

When the language is javascript, the "this" identifier has the type of the literal and this.a is a number.
The same is expected for typescript, but in typescript mode this is any and this.a is "No definition found for 'a'"

Tested with all extensions disabled.
Switching the language mode does not change the behavior, it depends only on the file extension "js" versus "ts".

The expectation is that in typescript mode, the behavior is the same as in javascript and that members are recognized. The type of f() should be a method returning a number. Currently it is recognized as any:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions