Title: Template compiler should treat TypeScript `!` on inputs as required inputs · Issue #24879 · angular/angular · GitHub
Open Graph Title: Template compiler should treat TypeScript `!` on inputs as required inputs · Issue #24879 · angular/angular
X Title: Template compiler should treat TypeScript `!` on inputs as required inputs · Issue #24879 · angular/angular
Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [ ] Performance issue [X] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submi...
Open Graph Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [ ] Performance issue [X] Feature request [ ] Documentation issue or request [ ...
X Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [ ] Performance issue [X] Feature request [ ] Documentation issue or reques...
Opengraph URL: https://github.com/angular/angular/issues/24879
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Template compiler should treat TypeScript `!` on inputs as required inputs","articleBody":"## I'm submitting a...\r\n\u003cpre>\u003ccode>\r\n[ ] Regression (a behavior that used to work and stopped working in a new release)\r\n[ ] Bug report \u003c!-- Please search GitHub for a similar issue or PR before submitting -->\r\n[ ] Performance issue\r\n[X] Feature request\r\n[ ] Documentation issue or request\r\n[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\r\n[ ] Other... Please describe:\r\n\u003c/code>\u003c/pre>\r\n\r\n## Current behavior\r\nCurrently, you cannot mark an `@Input()` as required a template compile time, you can just assert it is not undefined at runtime.\r\n\r\nTypeScript supports (or requires, if you use `strictPropertyInitialization`) adding a `!` to the end of instance property declarations which aren't initialized in-line or in the constructor to communicate to the type-checker that this variable is indeed defined, even if it doesn't appear to be.\r\n\r\nThe combination of the above two points means you end up with a sort of inconsistent state, in which you want to assert your inputs are defined in ngOnInit or ngOnChanges, yet the type-checker thinks they are already defined because you used the `!` operator on their definitions.\r\n\r\n## Expected behavior\r\nIt would be nice if the template compiler could detect this operator on declarations of inputs, and take it to mean that input is required. This means the template compiler would give a (helpful) error if you used such a component but did not specify one of these required inputs, avoiding such mistakes at compile time instead of at runtime.\r\n\r\n## Minimal reproduction of the problem with instructions\r\nhttp://plnkr.co/edit/7wr39D8FiEIb6JCteXr1?p=preview\r\n(stackblitz doesn't support a new enough version of TypeScript, so I'm using plnkr)\r\n\r\n## What is the motivation / use case for changing the behavior?\r\nIn general, it would be useful to have a required parameter check as a compile-time check instead of a runtime check to both catch mistakes earlier and to reduce/simplify code (no manual checks and having to deal with checking in both ngOnInit and onChanges).\r\n\r\nThis is especially true now that TypeScript supports `strictPropertyInitialization`, which makes the aforementioned checks less idiomatic for TypeScript to do (since you assume the value is defined but check if it isn't defined).\r\n\r\n\r\n## Environment\r\n\r\n\u003cpre>\u003ccode>\r\nTypeScript version 2.7+\r\n","author":{"url":"https://github.com/jcomputer","@type":"Person","name":"jcomputer"},"datePublished":"2018-07-13T20:44:17Z","url":"https://github.com/angular/angular/issues/24879"}
| 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:a4684234-f27d-13d8-a2a0-bdd99146501f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE48:19F84C:11FEEBC:170E3BB:6B010A4A |
| html-safe-nonce | 89ae965d6244d59bed6634b0c473d915654267c7dee60710b3275b7fb9314328 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTQ4OjE5Rjg0QzoxMUZFRUJDOjE3MEUzQkI6NkIwMTBBNEEiLCJ2aXNpdG9yX2lkIjoiMjEyMzg0MTU4OTk5NzUzNzg2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cf85cd9819d8e88b973c468a99f4caa03aa970af0132b2b948db84812a1ce5ba |
| hovercard-subject-tag | issue:341154052 |
| 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/angular/angular/24879/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f8f0bb0b3e306286fde31f227f8e46a93eddc2ba1fba500bcc11181cd9512084/angular/angular/issues/24879 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f8f0bb0b3e306286fde31f227f8e46a93eddc2ba1fba500bcc11181cd9512084/angular/angular/issues/24879 |
| og:image:alt | I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [ ] Performance issue [X] Feature request [ ] Documentation issue or request [ ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jcomputer |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width