Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
snuup opened this issue Feb 14, 2025 · 2 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@snuup
Copy link

snuup commented Feb 14, 2025

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

@mjbvz mjbvz transferred this issue from microsoft/vscode Feb 19, 2025
@mjbvz mjbvz removed their assignment Feb 19, 2025
@snuup
Copy link
Author

snuup commented Mar 1, 2025

any chance ti fix that? if not i will use classes instead of object literals.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Mar 4, 2025
@RyanCavanaugh
Copy link
Member

This works fine for me in the Playground, not sure what you're seeing there. Please open a new bug in this repo with the template filled in

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants