Title: Flow type information across *ngFor-like directives · Issue #10510 · angular/angular · GitHub
Open Graph Title: Flow type information across *ngFor-like directives · Issue #10510 · angular/angular
X Title: Flow type information across *ngFor-like directives · Issue #10510 · angular/angular
Description: Alternative proposal for the: Allow template local variables to publish type information so that IDEs can provide better completion Thanks to @chuckjaz for the clever idea. class Person { first: string; last:string; } class MyComp { item...
Open Graph Description: Alternative proposal for the: Allow template local variables to publish type information so that IDEs can provide better completion Thanks to @chuckjaz for the clever idea. class Person { first: st...
X Description: Alternative proposal for the: Allow template local variables to publish type information so that IDEs can provide better completion Thanks to @chuckjaz for the clever idea. class Person { first: st...
Opengraph URL: https://github.com/angular/angular/issues/10510
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Flow type information across *ngFor-like directives","articleBody":"Alternative proposal for the: [Allow template local variables to publish type information so that IDEs can provide better completion](https://github.com/angular/angular/issues/4850)\n\nThanks to @chuckjaz for the clever idea.\n\n```\nclass Person {\n first: string;\n last:string;\n}\n\nclass MyComp {\n items: string[];\n people: Person[];\n}\n\n\n/**\n * Assume we have template such as this:\n * \u003cdiv *ngFor=\"let person of people; trackBy: foo\">\n * {{people.length}}{{item.first}}\n * \u003c/div>\n */\n\n// These global variables are here only to flow the type information.\nvar _comp: MyComp;\nvar _peopleLetType = NgFor.extractTypeFromOf(_comp && _comp.people);\n\nclass MyComponentFactory{\n context: MyComp;\n ngFor: NgFor\u003ctypeof _peopleLetType> = new NgFor\u003ctypeof _peopleLetType>(new DivInnerFactory());\n create() {\n this.ngFor.of = this.context.people;\n }\n}\n\nclass DivInnerFactory {\n context: NgForRow\u003ctypeof _peopleLetType>;\n text: string;\n create() {\n this.context.$implicit.first; // expected type OK because $implicit is Person\n this.context.$implicit.name; // expected type error because $implicit is Person\n }\n}\n\nclass NgForRow\u003cT> {\n $implicit: T;\n}\n\n@Component({\n // we need some way to hint to the template generated which property to use for type extraction\n // and which static function to use with that property.\n typeTransformation: ['of', NgFor.extractTypeFromOf]\n})\nclass NgFor\u003cT>{\n @Input({typeTransform: NgFor.extractTypeFromOf})\n of: T[];\n constructor(factory: Factory) {}\n static extractTypeFromOf\u003cT>(expr: T[]|Iterable\u003cT>): T {return undefined;}\n}\n\ninterface Factory {\n create();\n}\ninterface Iterable\u003cT> {}\nfunction Component(a:any): Function { return null; }\nfunction Input(a:any): Function { return null; }\n```\n\nTry it out in [TypeScript playground](http://www.typescriptlang.org/play/index.html#src=class%20Person%20%7B%0D%0A%09first%3A%20string%3B%0D%0A%09last%3Astring%3B%0D%0A%7D%0D%0A%0D%0Aclass%20MyComp%20%7B%0D%0A%09items%3A%20string%5B%5D%3B%0D%0A%09people%3A%20Person%5B%5D%3B%0D%0A%7D%0D%0A%0D%0A%0D%0A%2F**%0D%0A%20*%20Assume%20we%20have%20template%20such%20as%20this%3A%0D%0A%20*%20%3Cdiv%20*ngFor%3D%22let%20person%20of%20people%3B%20trackBy%3A%20foo%22%3E%0D%0A%20*%20%20%20%7B%7Bpeople.length%7D%7D%7B%7Bitem.first%7D%7D%0D%0A%20*%20%3C%2Fdiv%3E%0D%0A%20*%2F%0D%0A%0D%0A%2F%2F%20These%20global%20variables%20are%20here%20only%20to%20flow%20the%20type%20information.%0D%0Avar%20_comp%3A%20MyComp%3B%0D%0Avar%20_peopleLetType%20%3D%20NgFor.extractTypeFromOf%28_comp%20%26%26%20_comp.people%29%3B%0D%0A%0D%0Aclass%20MyComponentFactory%7B%0D%0A%09context%3A%20MyComp%3B%0D%0A%09ngFor%3A%20NgFor%3Ctypeof%20_peopleLetType%3E%20%3D%20new%20NgFor%3Ctypeof%20_peopleLetType%3E%28new%20DivInnerFactory%28%29%29%3B%0D%0A%09create%28%29%20%7B%0D%0A%09%09this.ngFor.of%20%3D%20this.context.people%3B%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aclass%20DivInnerFactory%20%7B%0D%0A%09context%3A%20NgForRow%3Ctypeof%20_peopleLetType%3E%3B%0D%0A%09text%3A%20string%3B%0D%0A%09create%28%29%20%7B%0D%0A%09%09this.context.%24implicit.first%3B%20%2F%2F%20expected%20type%20OK%20because%20%24implicit%20is%20Person%0D%0A%09%09this.context.%24implicit.name%3B%20%20%2F%2F%20expected%20type%20error%20because%20%24implicit%20is%20Person%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aclass%20NgForRow%3CT%3E%20%7B%0D%0A%09%24implicit%3A%20T%3B%0D%0A%7D%0D%0A%0D%0A%40Component%28%7B%0D%0A%09%2F%2F%20we%20need%20some%20way%20to%20hint%20to%20the%20template%20generated%20which%20property%20to%20use%20for%20type%20extraction%0D%0A%09%2F%2F%20and%20which%20static%20function%20to%20use%20with%20that%20property.%0D%0A%09typeTransformation%3A%20%5B'of'%2C%20NgFor.extractTypeFromOf%5D%0D%0A%7D%29%0D%0Aclass%20NgFor%3CT%3E%7B%0D%0A%09%40Input%28%7BtypeTransform%3A%20NgFor.extractTypeFromOf%7D%29%0D%0A%09of%3A%20T%5B%5D%3B%0D%0A%09constructor%28factory%3A%20Factory%29%20%7B%7D%0D%0A%09static%20extractTypeFromOf%3CT%3E%28expr%3A%20T%5B%5D%7CIterable%3CT%3E%29%3A%20T%20%7Breturn%20undefined%3B%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20Factory%20%7B%0D%0A%09create%28%29%3B%0D%0A%7D%0D%0Ainterface%20Iterable%3CT%3E%20%7B%7D%0D%0Afunction%20Component%28a%3Aany%29%3A%20Function%20%7B%20return%20null%3B%20%7D%0D%0Afunction%20Input%28a%3Aany%29%3A%20Function%20%7B%20return%20null%3B%20%7D).\n","author":{"url":"https://github.com/mhevery","@type":"Person","name":"mhevery"},"datePublished":"2016-08-04T23:42:58Z","url":"https://github.com/angular/angular/issues/10510"}
| 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:1c316617-3995-26dc-6462-4e557fc4c4e8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E3C4:62283:CC0EE1:10FAB55:6B00F7A2 |
| html-safe-nonce | facc60aa3bd80200d25e95767a1d765f5cae5d020627ef814b38da29caa209ba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFM0M0OjYyMjgzOkNDMEVFMToxMEZBQjU1OjZCMDBGN0EyIiwidmlzaXRvcl9pZCI6IjIyMTIxNzQzMTQwNjM1OTEzMzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 16fd5edfe0e81931343e4025716c32a287d69ce310281a8fa94dd4ced20a39be |
| hovercard-subject-tag | issue:169499040 |
| 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/10510/issue_layout |
| twitter:image | https://opengraph.githubassets.com/731eefea10be8106399fcb1c48c02219431d928e8d7a66812cd85e95da2181a6/angular/angular/issues/10510 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/731eefea10be8106399fcb1c48c02219431d928e8d7a66812cd85e95da2181a6/angular/angular/issues/10510 |
| og:image:alt | Alternative proposal for the: Allow template local variables to publish type information so that IDEs can provide better completion Thanks to @chuckjaz for the clever idea. class Person { first: st... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mhevery |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width