-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Contextually type this in object literals in JS #17252
Contextually type this in object literals in JS #17252
Conversation
Previously, `this` would only get a contextual type inside object literals with `--noImplicitThis` turned on in Typescript files.
This is likely too restrictive - I've previously argued that what you really want is the contextual type |
Isn't const o = {
p: 12,
m() {
return this.p + this.x
}
} And |
method() { | ||
this; | ||
this.prop; | ||
this.method; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for unknown property. Should be no error, get any
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a sucker for changes that are only one line before tests. Nice one!
From conversation with @andy-ms, note that if you make a call like this: transformsPropertiesIntoMethods({
p: 12,
m() {
return this.p()
}
}) Then you will get an error with |
Previously,
this
would only get a contextual type inside object literals with--noImplicitThis
turned on in Typescript files.Fixes #16184