Type 'this' in function properties of object literals#8382
Conversation
Previously, methods of object literals would give a type to 'this'. Now function properties of object literals also give a type to 'this'.
|
👍 |
| n: 101, | ||
| explicitThis: function (m) { | ||
| return m + this.n.length; // ok, this.n: any | ||
| return m + this.n.length; // error, 'length' does not exist on 'number' |
There was a problem hiding this comment.
These comments are unhelpful to future test updates, please just remove them instead of changing them in the future.
There was a problem hiding this comment.
What I mean is, you run the tests, find out something is wrong, realize you have to update the test itself as well because the comment is now lying to you, and also update the baselines as well.
There was a problem hiding this comment.
What about the case that you run the tests, see that it changed, and have to figure out whether or not the change is an improvement? In that case the comment helps you figure out if the test should still pass, and why.
I've hit several old-ish tests that have accumulated multiple failures -- I couldn't tell if they were supposed to fail or even what the test was supposed to show.
|
👍 |
|
Hi @sandersn , I trying use that feature with typescript@2.1.0-dev.20160714 but stills I mean you PR gives: this: { x: number, m(n: number) : number, f(n: number): number }I'm right? There is something in Thanks for you PR! |
|
Unfortunately, we rolled back this change after some Ember and Dojo folks pointed out that it breaks common construction patterns where a function extends an object literal argument with some properties. TypeScript doesn't have a way to type that pattern at all today, so we decided to go back to See the discussion in #8191 for details. |
Fixes #8110
Previously, methods of object literals would give a type to
this. Now function properties of object literals also give a type tothis.In other words, there are no more
anytypes in the below sample. Previouslyf: (n: number) => anybecausethis: anyinside its body: