Title: [Proposal] Normalize inputs (@Input()) · Issue #21143 · angular/angular · GitHub
Open Graph Title: [Proposal] Normalize inputs (@Input()) · Issue #21143 · angular/angular
X Title: [Proposal] Normalize inputs (@Input()) · Issue #21143 · angular/angular
Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submit support request here...
Open Graph Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request => P...
X Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request ...
Opengraph URL: https://github.com/angular/angular/issues/21143
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Proposal] Normalize inputs (@Input())","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[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\u003c/code>\u003c/pre>\r\n\r\n## Problem\r\n\r\n1) There isn't type check on `@Input` so I tend to expect any type\r\n2) sometimes you want to give a flexible API to user by using input like \r\n \r\n ```\r\n @Input() in: string | string[] | Foo[]\r\n ````\r\n but internally you just want to manage `Foo[]`.\r\n\r\n*Edit: 1) seems to be possible since v5*\r\n\r\n## Solution\r\nIntroduce normalizer function to use on `@Input()` decorator. \r\n```ts\r\ntype NormalizerFn = (v: any) => any;\r\n\r\nInput(name: string);\r\nInput(normalizer: NormalizerFn);\r\nInput(name: string, normalizer: NormalizerFn);\r\n```\r\nThe normalizer function should be pure. Here is an example : \r\n```\r\nconst asBoolean = v => v === true;\r\n```\r\n\r\nusage \r\n```ts\r\n@Input(NotNull)\r\nv: string\r\n\r\n@Input('value', asBoolean)\r\nw: boolean\r\n```\r\n\r\nWe could provide the most commons one and a `compose()` utility so we can compose multiple normalizer into a new one.\r\n\r\nI've done a POC showing it all here : https://stackblitz.com/edit/angular-input-with-normalizer?file=app%2Fapp.component.ts\r\n\r\nSo this POC is just reducing the boilerplate of create a setter to do the job but I think this should be taken care of by Angular itself prior to set the input on the component.\r\n\r\n## Benefits\r\n\r\n1) Remove the need of all those setter/getter which are tedious and introduce a cleaner and flexible way to do it.\r\n2) Enforce OnPush strategy, changing an input from 2 values that get normalized to the same final value won't trigger CD anymore.\r\n3) Probably harmonize the way we handle certain type like boolean (see #19929)\r\n\r\n## Problem\r\nI'm not sure if the compiler can pick up a function definition inside metas and run it elsewhere. I'm hoping for someone with the knowledge to step in and give us a brief explanation before I start digging on the subject. \r\n\r\nI don't think this introduce breaking change but not sure.\r\n\r\nRelated to #19929 and #8968.","author":{"url":"https://github.com/ghetolay","@type":"Person","name":"ghetolay"},"datePublished":"2017-12-21T22:41:04Z","url":"https://github.com/angular/angular/issues/21143"}
| 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:788e78fa-1cca-6d89-1e1a-75e633787dbe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C8F0:3FEC83:AD736:E1CE1:6B015E72 |
| html-safe-nonce | 7ac05fea274d17ddcd4c895cd063c35807a4d4696689d603bff727502de31594 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOEYwOjNGRUM4MzpBRDczNjpFMUNFMTo2QjAxNUU3MiIsInZpc2l0b3JfaWQiOiI4OTU4MDk5ODkzNzYwNDA1NjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c23e29dcfce215216cbb4bc6513149cf94b7b004f82555b179e6ccb6bc1857f6 |
| hovercard-subject-tag | issue:284025009 |
| 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/21143/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9c6ace237a5676b0769a357eac7bf0542ebe37d04a807ef3297e417f809c31c1/angular/angular/issues/21143 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9c6ace237a5676b0769a357eac7bf0542ebe37d04a807ef3297e417f809c31c1/angular/angular/issues/21143 |
| og:image:alt | I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request => P... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ghetolay |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width