-
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
Option to make this
in functions inside object literals typed
#12785
Comments
|
We have tried changing this behavior a while back and ran into some interesting scenarios that would not work, some relevant comments in #7801 (comment) |
@RyanCavanaugh: I have updated the examples to use @mhegazy: I have read the comments and understand that |
I think it's badly needed! It's too terrible to create an interface for every Object literal! And I hate that I must declare a new member function whenever I want to add it to the origin literal. As for POJO, well, Anyone has a forked version of TypeScript 2 and the linter which support "auto-typed" |
See #14141. |
I think the suggested feature is no longer necessary. |
The lack of IntelliSense to
this
in functions inside object literals was reported in #7801 and #8110. #8382 attempted to fix that but it was reverted via #8389 due to #8191.Adding
this: this
to functions in an interface, as mentioned in #9583, was a suggested way to workaround this issue. However, it is not convenient to definethis: this
for each method of an interface. Also, even when the object implements an interface, it may contain additional properties that are used in the interface methods but not exposed via the interface. Finally, like microsoft/vscode#298, there may be no interface at all.Although typing
this
in functions inside object literals cannot be switched on by default, I think there should be a way to opt in. Could something be applied to an object literal to explicitly markthis
in all methods of that object be the type of the surrounding object?In the following examples,
@
is a symbol thatthis
in all methods of that object is of the type of the surrounding object.this:
in a method declaration inside an object literal,this
in that method is of the type of the surrounding object.Example 1--
this
being the surrounding object in all methods of an object literal:Example 2--
this
being the surrounding object in a method of an object literal:Example 3--
this
being the surrounding object in all methods of an object literal that implements other interfaces:The text was updated successfully, but these errors were encountered: