Title: Suggestion: when compiling with noImplicityAny make 'this' have type '{}' where it previously had type 'any' · Issue #1740 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: when compiling with noImplicityAny make 'this' have type '{}' where it previously had type 'any' · Issue #1740 · microsoft/TypeScript
X Title: Suggestion: when compiling with noImplicityAny make 'this' have type '{}' where it previously had type 'any' · Issue #1740 · microsoft/TypeScript
Description: This would be a breaking change, so might need to be a separate flag. However I suspect anyone using noImplicitAny would switch on that flag, so maybe it should just be normal behaviour for noImplicitAny. If inside a class method foo I w...
Open Graph Description: This would be a breaking change, so might need to be a separate flag. However I suspect anyone using noImplicitAny would switch on that flag, so maybe it should just be normal behaviour for noImpli...
X Description: This would be a breaking change, so might need to be a separate flag. However I suspect anyone using noImplicitAny would switch on that flag, so maybe it should just be normal behaviour for noImpli...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/1740
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: when compiling with noImplicityAny make 'this' have type '{}' where it previously had type 'any'","articleBody":"This would be a breaking change, so might need to be a separate flag. However I suspect anyone using noImplicitAny would switch on that flag, so maybe it should just be normal behaviour for noImplicitAny.\n\nIf inside a class method foo I write:\n\n```\nfoo() {\n $.get('/blah', blah => this.blah = blah);\n}\n```\n\nThe lambda correctly preserves the type of `this` to be the surrounding class, and so my class better have a property `blah` that is of the right type or the compiler will complain. All good.\n\nIf instead I write:\n\n```\nfoo() {\n $.get('/blah', function() { this.blah = blah; });\n}\n```\n\nNow `this` no longer refers to the class object, so it no longer works. This is fine - function has different behaviour.\n\nThe problem is that `this` has been given the type `any`.\n\nYou might argue that I asked the compile to do that by saying `function() {...` instead of using a lambda, so this wasn't an _implicit_ any, as such.\n\nYes, in that example the problem is easy to spot thanks to the `function` keyword. But how about:\n\n```\nfoo() {\n this.mouseEvents = {\n down() {\n this.mouseDown = true;\n },\n up() {\n this.mouseDown = false;\n }\n }\n}\n```\n\nNo `function` keywords, because I'm using ES6's nice abbreviation for an object literal with functions. Inside `down()`, `this` is again not the surrounding object but is the new object I'm assigning to `this.mouseEvents`. Sometimes that's what I want, but in this case it's not. The problem is again that `this` is of type `any` so the compiler checks nothing.\n\nSo the suggestion is to give `this` the type `{}` in these circumstances. In the (hopefully rare) cases where I genuinely do want to smuggle information into a function via untyped `this`, I will need to do my own explicit declaration at the start of the function:\n\n```\nvar extraInfo: any = this;\n```\n\nBut if it's a mistake (most cases), I'll find out when I try to access my class's properties/methods on `{}`.\n\nFor bonus points, in the object literal example, the type of `this` could be the type of the object literal (that's what it is at runtime). But I guess that might raise circularity difficulties. The core problem here is the implicit introduction of `this: any`.\n","author":{"url":"https://github.com/danielearwicker","@type":"Person","name":"danielearwicker"},"datePublished":"2015-01-20T12:46:08Z","url":"https://github.com/microsoft/TypeScript/issues/1740"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5d7d977b-d576-87c6-c09b-deb89e82ed14 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 97A2:5885B:80C243:B2CD2B:6B00A1E9 |
| html-safe-nonce | c2ca7cb000e153ace7cd4b39f55ebb8b8e93397d878d54b5cbb726278bd13892 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0EyOjU4ODVCOjgwQzI0MzpCMkNEMkI6NkIwMEExRTkiLCJ2aXNpdG9yX2lkIjoiODc3MTI4NDYzMTQ3NjU0Mzk3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e206d7bbce9a1651f1d9473f5a88dc7d8322dbe904c26295ed3a4282717c295b |
| hovercard-subject-tag | issue:54878878 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/1740/issue_layout |
| twitter:image | https://opengraph.githubassets.com/00d2f1600f81b79644c8b1197b63b610231628a4df0cf0d8b5e451787f8cfeeb/microsoft/TypeScript/issues/1740 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/00d2f1600f81b79644c8b1197b63b610231628a4df0cf0d8b5e451787f8cfeeb/microsoft/TypeScript/issues/1740 |
| og:image:alt | This would be a breaking change, so might need to be a separate flag. However I suspect anyone using noImplicitAny would switch on that flag, so maybe it should just be normal behaviour for noImpli... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danielearwicker |
| hostname | github.com |
| expected-hostname | github.com |
| None | 52c95ce7e48016ae14399656b4ee812462b6f1fa28c5008c8394da64794c00b8 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1c9ddec8215f9a94e295c8310c3579b9c54ba31a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width