Title: Flow type information across *ngFor-like directives · Issue #12398 · angular/angular · GitHub
Open Graph Title: Flow type information across *ngFor-like directives · Issue #12398 · angular/angular
X Title: Flow type information across *ngFor-like directives · Issue #12398 · angular/angular
Description: This issues supersedes and replaces the proposals in #10510 and #4850. The problem Assume the following code example: @Component({ template: `
Open Graph Description: This issues supersedes and replaces the proposals in #10510 and #4850. The problem Assume the following code example: @Component({ template: `
X Description: This issues supersedes and replaces the proposals in #10510 and #4850. The problem Assume the following code example: @Component({ template: ` <ul> <li *ngFor="let person of people; l...
Opengraph URL: https://github.com/angular/angular/issues/12398
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Flow type information across *ngFor-like directives","articleBody":"This issues supersedes and replaces the proposals in #10510 and #4850.\n# The problem\n\nAssume the following code example:\n\n``` ts\n@Component({\n template: `\n \u003cul>\n \u003cli *ngFor=\"let person of people; let i = index\">{{i + 0}}: {{person.last}}, {{person.first}}\u003cli>\n \u003cul>\n`\n})\nclass MyExample {\n people: Person[] = [];\n}\n\nclass Person {\n first: string;\n last: string;\n}\n```\n\nIn Angular 2.1, the generated code for `person.first` is not type checked because the type of `person` is generated as `any`. The type of `person` should be `Person`. This would also allow the Angular language service to supply `first` and `last` as completions for the expression `person.` in the interpolations.\n# Solution\n1. Change type of `NgFor` and `NgForRow` to take a type parameter `T`.\n2. Change type of `ngForOf` to be `T[] | Iterable\u003cT>`. \n3. Change `$implicit` in `NgForRow` to be of type `T`.\n4. Modify the code generation to flow the type from the input bindings to the type parameters.\n\nThe modified `NgFor` and `NgForRow` would look like:\n\n``` ts\nexport class NgForRow\u003cT> {\n constructor(public $implicit: T, public index: number, public count: number) {}\n ...\n}\n\n@Directive({selector: '[ngFor][ngForOf]'})\nexport class NgFor\u003cT> implements DoCheck, OnChanges {\n @Input() ngForOf: T[] ;\n @Input() ngForTrackBy: TrackByFn;\n\n private _differ: IterableDiffer = null;\n\n constructor(\n private _viewContainer: ViewContainerRef, private _template: TemplateRef\u003cNgForRow\u003cT>>,\n private _differs: IterableDiffers, private _cdr: ChangeDetectorRef) {}\n\n @Input()\n set ngForTemplate(value: TemplateRef\u003cNgForRow\u003cT>>) {\n if (value) {\n this._template = value;\n }\n }\n\n ...\n}\n```\n\nHere is now a similar example from the above could be generated that flows the types from the input properties to the type parameters:\n\n``` ts\n// User Provided types\nclass Person {\n first: string;\n last:string;\n}\n\nclass MyComp {\n items: string[];\n people: Person[];\n}\n\n\n// Types representing relavant parts of the corresponding types in Angular\nclass AppView\u003cT> {\n context: T;\n}\n\ninterface TemplateRef\u003cT> { create(context: T): ViewRef\u003cT>; contextType: T }\ninterface ViewRef\u003cT> { context: T }\n\n\nclass NgForRow\u003cT> {\n $implicit: T;\n index: number;\n}\n\nclass NgFor\u003cT>{\n of: T[];\n constructor(template: TemplateRef\u003cNgForRow\u003cT>>) { }\n}\n\n/**\n * Assume we have template such as this:\n * \u003cdiv *ngFor=\"let person of people; let index = index\">\n * {{index}}: {{person.first}}\n * \u003c/div>\n */\n\n// We can auto generate the following function which captures the\n// type relation between `of` property and `template`.\n\nfunction NgFor_Template\u003cT>(of: T[]): TemplateRef\u003cNgForRow\u003cT>> { return undefined as any; }\nfunction NgFor_T\u003cT>(of: T[]): T { return undefined as any; }\n\n// These global variables are here only to flow the type information.\nvar _comp: MyComp;\n\n// The template row can be constructed as:\nvar template = NgFor_Template(_comp && _comp.people);\nvar row: typeof template.contextType;\n\nclass RowView extends AppView\u003ctypeof row> {\n}\nvar rowView = new RowView();\n\n// Note the type of person is People\nvar person = rowView.context.$implicit;\n\n// The correct NgFor can be constructed as:\nvar p1 = NgFor_T(_comp && _comp.people);\nvar ngFor = new NgFor\u003ctypeof p1>(template);\n```\n\nTry it in the [TypeScript playground](http://www.typescriptlang.org/play/index.html#src=%2F%2F%20User%20Provided%20types%0D%0Aclass%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%2F%20Types%20representing%20relavant%20parts%20of%20the%20corresponding%20types%20in%20Angular%0D%0Aclass%20AppView%3CT%3E%20%7B%0D%0A%09context%3A%20T%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20TemplateRef%3CT%3E%20%7B%20create%28context%3A%20T%29%3A%20ViewRef%3CT%3E%3B%20contextType%3A%20T%20%7D%0D%0Ainterface%20ViewRef%3CT%3E%20%7B%20context%3A%20T%20%7D%0D%0A%0D%0A%0D%0Aclass%20NgForRow%3CT%3E%20%7B%0D%0A%09%24implicit%3A%20T%3B%0D%0A%09index%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Aclass%20NgFor%3CT%3E%7B%0D%0A%09of%3A%20T%5B%5D%3B%0D%0A%09constructor%28template%3A%20TemplateRef%3CNgForRow%3CT%3E%3E%29%20%7B%20%7D%0D%0A%7D%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%20let%20index%20%3D%20index%22%3E%0D%0A%20*%20%20%20%7B%7Bindex%7D%7D%3A%20%7B%7Bperson.first%7D%7D%0D%0A%20*%20%3C%2Fdiv%3E%0D%0A%20*%2F%0D%0A%0D%0A%2F%2F%20We%20can%20auto%20generate%20the%20following%20funciton%20which%20captures%20the%0D%0A%2F%2F%20type%20relation%20between%20%60of%60%20property%20and%20%60template%60.%0D%0A%0D%0Afunction%20NgFor_Template%3CT%3E%28of%3A%20T%5B%5D%29%3A%20TemplateRef%3CNgForRow%3CT%3E%3E%20%7B%20return%20undefined%20as%20any%3B%20%7D%0D%0Afunction%20NgFor_T%3CT%3E%28of%3A%20T%5B%5D%29%3A%20T%20%7B%20return%20undefined%20as%20any%3B%20%7D%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%0A%0D%0A%2F%2F%20The%20template%20row%20can%20be%20constructed%20as%3A%0D%0Avar%20template%20%3D%20NgFor_Template%28_comp%20%26%26%20_comp.people%29%3B%0D%0Avar%20row%3A%20typeof%20template.contextType%3B%0D%0A%0D%0Aclass%20RowView%20extends%20AppView%3Ctypeof%20row%3E%20%7B%0D%0A%7D%0D%0Avar%20rowView%20%3D%20new%20RowView%28%29%3B%0D%0A%0D%0A%2F%2F%20Note%20the%20type%20of%20person%20is%20People%0D%0Avar%20person%20%3D%20rowView.context.%24implicit%3B%0D%0A%0D%0A%2F%2F%20The%20correct%20NgFor%20can%20be%20constructed%20as%3A%0D%0Avar%20p1%20%3D%20NgFor_T%28_comp%20%26%26%20_comp.people%29%3B%0D%0Avar%20ngFor%20%3D%20new%20NgFor%3Ctypeof%20p1%3E%28template%29%3B%0D%0A).\n","author":{"url":"https://github.com/chuckjaz","@type":"Person","name":"chuckjaz"},"datePublished":"2016-10-19T23:48:16Z","url":"https://github.com/angular/angular/issues/12398"}
| 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:dc7aa7a7-fa3f-6b85-0b27-176b3eddb9d2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA10:3D8CBB:CA9915:10CAA55:6B00F571 |
| html-safe-nonce | fc0986c6144457425e61889f94cd649a4720916565e078346c1ac3c6b33b2fa5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTEwOjNEOENCQjpDQTk5MTU6MTBDQUE1NTo2QjAwRjU3MSIsInZpc2l0b3JfaWQiOiI0NDg2OTMyMzUxNDQzNTk2NjU3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 161c749c0c506ea26160de94eb7bcf342a1436872c87a577b574fe8eaa5c2637 |
| hovercard-subject-tag | issue:184102179 |
| 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/12398/issue_layout |
| twitter:image | https://opengraph.githubassets.com/91b02f36d538d0d25c7a641f4e0ebb88abb6d1afaf43289bf5a637ef6af10666/angular/angular/issues/12398 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/91b02f36d538d0d25c7a641f4e0ebb88abb6d1afaf43289bf5a637ef6af10666/angular/angular/issues/12398 |
| og:image:alt | This issues supersedes and replaces the proposals in #10510 and #4850. The problem Assume the following code example: @Component({ template: `
|
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chuckjaz |
| 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