Title: Provide better type safety for viewChild (and viewChildren) · Issue #59717 · angular/angular · GitHub
Open Graph Title: Provide better type safety for viewChild (and viewChildren) · Issue #59717 · angular/angular
X Title: Provide better type safety for viewChild (and viewChildren) · Issue #59717 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core Description At the moment, it is easy to incorrectly type a viewChild() (or viewChildren()) signal. For example: readonly myElement = viewChild Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Description At the moment, it is easy to incorrectly type a viewChild() (or viewChildren()) signal. For example: readon...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Description At the moment, it is easy to incorrectly type a viewChild() (or viewChildren()) signal. For example: readon...
Opengraph URL: https://github.com/angular/angular/issues/59717
X: @github
Domain: github.com
Links:
Viewport: width=device-width
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Provide better type safety for viewChild (and viewChildren)","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncore\n\n### Description\n\nAt the moment, it is easy to incorrectly type a `viewChild()` (or `viewChildren()`) signal. For example:\n\n```\nreadonly myElement = viewChild\u003cHTMLInputElement>('someSelector');\n```\n\nHere I am passing a generic argument, HTMLInputElement, but the element may have a different type. Often I forget to wrap the element within an `ElementRef`, as I did here, but I digress. The point is, I often waste time being unsure of what is actually being injected by Angular in the component class.\n\n### Proposed solution\n\nI believe that the easiest solution would be to include some kind of validator function:\n```\nreadonly myElement = viewChild('someSelector', { validator: e => e instanceof HTMLInputElement });\n```\n\nType signature would be as follows:\n\n```\n\u003cT>(selector: string, options: { validator: (el: unknown) => el is T }) : Signal\u003cT>;\n```\n\n### Alternatives considered\n\n**Alternative 1:**\n\nThe current API is not as ergonomic as it could be. Why can't the Angular Language service ensure that `myElement` is of the correct type:\n```\nreadonly myElement: HTMLElement;\n```\n... and in the template:\n```\n\u003cinput #myElement /> \u003c!-- throw error here (if not of type HTMLElement) -->\n```\n\n**Alternative 2:**\n\nI thought about creating an external utility function that wraps the existing functionality but does an additional type check when the signal is invoked. I was unsuccessful. Perhaps you could tell me how to facilitate this. Or better, yet, perhaps you could build a function that facilitates this.","author":{"url":"https://github.com/StephenSPaul","@type":"Person","name":"StephenSPaul"},"datePublished":"2025-01-25T20:40:23Z","url":"https://github.com/angular/angular/issues/59717"}
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:19c9ac87-386c-de9c-a32b-398fbd480efd current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id 9A52:1F8F:A1870C:D37E9E:6B01383C html-safe-nonce 5bc3b770fa46f54270eb94506cb140e126f09031956b9fbd6e25c19ad9d25ef0 visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTUyOjFGOEY6QTE4NzBDOkQzN0U5RTo2QjAxMzgzQyIsInZpc2l0b3JfaWQiOiI0NjQwNzM0NTA3NTQzMTE1ODM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= visitor-hmac 0bce2ba2bcf81b052be8e0e8a8a375ce9f67b5a09c06fecd7bdde4748b617706 hovercard-subject-tag issue:2811166655 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/59717/issue_layout twitter:image https://opengraph.githubassets.com/aa3345f56744f8eabb974e26e285a6f5837351e8aaa423b7b223476ded320e1a/angular/angular/issues/59717 twitter:card summary_large_image og:image https://opengraph.githubassets.com/aa3345f56744f8eabb974e26e285a6f5837351e8aaa423b7b223476ded320e1a/angular/angular/issues/59717 og:image:alt Which @angular/* package(s) are relevant/related to the feature request? core Description At the moment, it is easy to incorrectly type a viewChild() (or viewChildren()) signal. For example: readon... og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username StephenSPaul 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
URLs of crawlers that visited me.