Title: Allow template local variables to publish type information so that IDEs can provide better completion · Issue #4850 · angular/angular · GitHub
Open Graph Title: Allow template local variables to publish type information so that IDEs can provide better completion · Issue #4850 · angular/angular
X Title: Allow template local variables to publish type information so that IDEs can provide better completion · Issue #4850 · angular/angular
Description: Problem Assume the following code example: @Component({ template: `
` }) class MyExample { users: List
Open Graph Description: Problem Assume the following code example: @Component({ template: `
` }) class MyExample { users...
X Description: Problem Assume the following code example: @Component({ template: ` <ul> <li *ngFor="let user of users; let i = index">{{i + 0}}: {{user.last}}, {{user.first}}<li> <u...
Opengraph URL: https://github.com/angular/angular/issues/4850
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow template local variables to publish type information so that IDEs can provide better completion","articleBody":"## Problem\n\nAssume the following code example:\n\n```\n@Component({\n template: `\n \u003cul>\n \u003cli *ngFor=\"let user of users; let i = index\">{{i + 0}}: {{user.last}}, {{user.first}}\u003cli>\n \u003cul>\n`\n})\nclass MyExample {\n users: List\u003cUser> = [];\n}\n\nclass User {\n first: string;\n last: string;\n}\n```\n\nHere the IDE has type information for `users`, but it does not have type information for `user` and `i`. For this reason it can not verify:\n- that `i+0` is valid because it does not know that `i` is a number.\n- that `user.last` and `user.first` are valid because it does not know what is the type for `user`\n## Solution\n\nParameterize `TemplateRef\u003cT>`.\n\n```\nclass Row {\n $implicit: any;\n index: number;\n even: boolean;\n odd: boolean;\n}\n\n@Directive({\n selector: '[ng-for][ng-for-of]',\n})\nclass NgFor {\n constructor(templateRef: TemplateRef\u003cRow>, viewContainer:ViewContainer) {\n } \n}\n\n```\n## Changing the View API\n\nCurrently setting the local variables is done as:\n\n```\nvar view: ViewRef;\nview.setLocal('$implicit', item)\n```\n\nA better way to do this, which is more consistent with the above proposal is to inject a locals object during view Creation.\n\n```\nvar local: Row = new Row(item, index);\nvar view: ViewRef\u003cRow> = this._viewContainer.createEmbeddedView(templateRef, index, local);\n```\n\nThe advantage is that currently `ngFor` has to compute `odd`, `even` regardless of wether it is needed in the template. By injecting a local's object the `odd` field can be computed.\n\n```\nclass Row implements Locals {\n constructor(public $implicit: T, index: number) {}\n\n get even() => this.index % 2 == 0;\n get odd() => this.index % 2 == 1;\n}\n```\n\nThis means that the `Row` object can have a rich API, but unless a field is actually referenced from the template it has no cost of computing these fields. Furthermore by using a typed object we can get better performance on locals then when using hashes. \n## Add Type to the templates\n\nRequires: read generics from DI dependencies (constructor arguments).\n\n``` typescript\nimport {User} from '...';\n\n@Component({\n types: {User}\n template: `\n \u003cul>\n \u003cli *ngFor=\"let user: User of users; let i = index\">{{i + 0}}: {{user.last}}, {{user.first}}\u003cli>\n \u003cul>\n`\n})\nclass MyExample {\n users: List\u003cUser> = [];\n}\n```\n\nNotice the `types` in the `@Component` which sets up a map of template types to real types. This will allow the compiler to generate code such as this.\n\n```\nGenerated code:\n\n// Deduce generic Row from NgFor constructor asking for a TemplateRef\u003cRow>\nclass MyExample_View1\u003cRow> extends AppView\u003cRow> {\n ...\n changeDetection() {\n var user: User /* retrieved from the Template */ = context.$implicit;\n var i = context.index; // Here i is number because context.index is number.\n }\n\n}\n\nclass AppView\u003cT> {\n context: T\n}\n\nclass TemplateRef\u003cT> {\n appView: AppView\u003cT>;\n get context(): T {\n return this.appView.context;\n }\n}\n```\n","author":{"url":"https://github.com/mhevery","@type":"Person","name":"mhevery"},"datePublished":"2015-10-21T16:33:44Z","url":"https://github.com/angular/angular/issues/4850"}
| 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:ef339b68-367a-daee-f302-a6f4f7346ab9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B18E:785A9:B2CD3B:E3DD5C:6B00F791 |
| html-safe-nonce | dc61358b64d3864707ccfd0771e7e06af044b6eb40193eca81d757519a4c3b51 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMThFOjc4NUE5OkIyQ0QzQjpFM0RENUM6NkIwMEY3OTEiLCJ2aXNpdG9yX2lkIjoiOTIyNjE4MzQ5NTgxMDg1NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 24f37401f0d167c29f835bb17dd8345580544d622ba285cc6e38ca1ddd352e35 |
| hovercard-subject-tag | issue:112629484 |
| 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/4850/issue_layout |
| twitter:image | https://opengraph.githubassets.com/95e26dacd5fd1d6c332aeb07b3d937a4e1b2ad52da10a12c636d2126fd787d61/angular/angular/issues/4850 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/95e26dacd5fd1d6c332aeb07b3d937a4e1b2ad52da10a12c636d2126fd787d61/angular/angular/issues/4850 |
| og:image:alt | 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 | 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