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

this in function declarations inside object literals should be typed #8110

Closed
sandersn opened this issue Apr 15, 2016 · 4 comments
Closed

this in function declarations inside object literals should be typed #8110

sandersn opened this issue Apr 15, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@sandersn
Copy link
Member

let o = {
  f: function () { this./**/ }
}

Expected: Intellisense shows 'f' in the list
Actual: Intellisense shows nothing because this: any.

Note that this similar code already works:

let o = {
  f() { this./**/ }
}
@sandersn
Copy link
Member Author

#8389 reverts this fix for now.

@sandersn sandersn reopened this Apr 30, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Apr 30, 2016

Just to clarify, both patterns will result in a this of type any.

We have tried different ways of addressing this issue, and the current behavior (this in an object literal is of type any) seems to be the most reasonable.

Classes present stronger assumptions about how the functions will be called. object literals on the other hand can be used as a property bag often with no assumptions about the relationship of the methods and properties on them. we have seen common JS patterns where methods are added, removed, or called with a different this target at runtime (see #8133 for an example).

the recommendation is to specify an explicit this type in methods:

interface FooBar {
    foo(): void;
    bar(): void;
}

let o = {
    foo: function (this: FooBar) { this /* FooBar */ },
    bar(this: FooBar) { this /* FooBar */ }
}

@mhegazy mhegazy closed this as completed Apr 30, 2016
@mhegazy mhegazy added the Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it label Apr 30, 2016
@FranklinWhale
Copy link

FranklinWhale commented Dec 8, 2016

Moved to #12785

@ahejlsberg
Copy link
Member

See #14141.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

4 participants