Title: Several issues with strict property checks and definite assignement assertion · Issue #20740 · microsoft/TypeScript · GitHub
Open Graph Title: Several issues with strict property checks and definite assignement assertion · Issue #20740 · microsoft/TypeScript
X Title: Several issues with strict property checks and definite assignement assertion · Issue #20740 · microsoft/TypeScript
Description: TypeScript Version: 2.7.0-dev.20171214 This issue is about the new strict property checks #20075 and definite assignement assertion #20166. Anders Hejlsberg (@ahejlsberg) Mohamed Hegazy (@mhegazy) Daniel Rosenwasser (@DanielRosenwasser) ...
Open Graph Description: TypeScript Version: 2.7.0-dev.20171214 This issue is about the new strict property checks #20075 and definite assignement assertion #20166. Anders Hejlsberg (@ahejlsberg) Mohamed Hegazy (@mhegazy) ...
X Description: TypeScript Version: 2.7.0-dev.20171214 This issue is about the new strict property checks #20075 and definite assignement assertion #20166. Anders Hejlsberg (@ahejlsberg) Mohamed Hegazy (@mhegazy) ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/20740
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Several issues with strict property checks and definite assignement assertion","articleBody":"**TypeScript Version:** 2.7.0-dev.20171214\r\n\r\nThis issue is about the new strict property checks #20075 and definite assignement assertion #20166.\r\n\r\n@ahejlsberg @mhegazy @DanielRosenwasser @RyanCavanaugh Did you guys test this new feature on real world projects ? Because as it is now, it could lead to a huge (negative ?) impact. It may need more thinking/fixing before landing in stable.\r\n\r\nAs VS Code Insiders now uses TS 2.7, when opening my Angular project (which is in strict mode), I discovered with surprise that now half my files contain errors (all my components) due to this new feature. And trying to get compliant with this led to many problems, some of them with no solution currently.\r\n\r\nTake this very simple Angular code : \r\n\r\n```typescript\r\nimport { Component, Input } from '@angular/core';\r\nimport { Movie } from './movie';\r\n\r\n@Component({\r\n selector: 'app-movie',\r\n template: `\u003cdiv>\u003ch1>{{movie.title}}\u003c/h1>\u003c/div>`\r\n})\r\nexport class MovieComponent {\r\n\r\n @Input() movie: Movie;\r\n\r\n}\r\n```\r\n\r\n`movie` property now raises an error as TS can't know it will be initialized by an Angular mecanism.\r\n\r\nLet's look at the solutions to be compliant with the new strict mode feature : \r\n\r\n1. Putting default values everywhere : \r\n```typescript\r\n@Input() movie: Movie | null = null;\r\n```\r\n\r\nUnfortunately, that's not possible here, as an object is expected. Only solution would be to set `null` by default, but then strict mode would raise another error due to `strictNullChecks`. So you would allow `null` as a type, but then TS will scream everywhere you use `movie` inside the class and force you to check if it's not null, when you already know very well it's not. So it's a mess.\r\n\r\nAnother problem : this feature checks the constructor, but in Angular you often must wait another callback like `ngOnInit` to do your initial stuff.\r\n\r\n2. Use the new definite assignement assertion : \r\n```typescript\r\n@Input() movie!: Movie;\r\n```\r\n\r\nFirst, in Angular, it would mean quite all the components properties will need this \"hack\". As in reality, the code is OK, it seems to me it's a too overwhelming solution to just calm down TypeScript. It could lead people to never adopt this feature, and then complicate the usage of the combined `strict` option, as we would need to just activate some individual strict features. Did you guys ask the Angular team what they think about this ?\r\n\r\nSecond, it's now Angular which raises an error in the template, saying `Identifier 'title' is not defined. '\u003canonymous>' does not contain such a member`. We should verify before landing that the Angular language service is able to fix this issue. @chuckjaz \r\n\r\nThird, TS Lint is not happy too : it's saying `Missing semicolon (semicolon)` just after the `!`. It also should be fixed before this feature is landing to stable.\r\n","author":{"url":"https://github.com/cyrilletuzi","@type":"Person","name":"cyrilletuzi"},"datePublished":"2017-12-17T18:52:52Z","url":"https://github.com/microsoft/TypeScript/issues/20740"}
| 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:80cc14ec-c463-20e6-2e77-dd000ee5f247 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 82EA:10DACD:6394A0:86592A:6B00D497 |
| html-safe-nonce | 4812e5dfbaae36c2979ba115ff42b7a5c2c9e0a32e62842331b8adfb9f0f6c5d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkVBOjEwREFDRDo2Mzk0QTA6ODY1OTJBOjZCMDBENDk3IiwidmlzaXRvcl9pZCI6IjgzNDI0NjIyNzE4NzE1NzEwOTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d30d0b001ab555fd089f54742b24e6b403d8cd9f727e245b1ff85d18c39f8b93 |
| hovercard-subject-tag | issue:282714920 |
| 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/20740/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e6e41143e4bf44f26f6595b6206fad1ac2743bda67ac832acf143d6158442a68/microsoft/TypeScript/issues/20740 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e6e41143e4bf44f26f6595b6206fad1ac2743bda67ac832acf143d6158442a68/microsoft/TypeScript/issues/20740 |
| og:image:alt | TypeScript Version: 2.7.0-dev.20171214 This issue is about the new strict property checks #20075 and definite assignement assertion #20166. Anders Hejlsberg (@ahejlsberg) Mohamed Hegazy (@mhegazy) ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cyrilletuzi |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width