Title: `inject` function does not correctly infer the return type when used with generic classes/abstract classes · Issue #48126 · angular/angular · GitHub
Open Graph Title: `inject` function does not correctly infer the return type when used with generic classes/abstract classes · Issue #48126 · angular/angular
X Title: `inject` function does not correctly infer the return type when used with generic classes/abstract classes · Issue #48126 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description If we use the inject function with a generic class (or abstract class), the return type won't be properly inferred: import { inject } from '...
Open Graph Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description If we use the inject function with a generic class (or abstract class), the return type won't be pro...
X Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description If we use the inject function with a generic class (or abstract class), the return type won't be...
Opengraph URL: https://github.com/angular/angular/issues/48126
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`inject` function does not correctly infer the return type when used with generic classes/abstract classes","articleBody":"### Which @angular/* package(s) are the source of the bug?\r\n\r\ncore\r\n\r\n### Is this a regression?\r\n\r\nNo\r\n\r\n### Description\r\n\r\nIf we use the `inject` function with a generic class (or abstract class), the return type won't be properly inferred:\r\n\r\n```ts\r\nimport { inject } from '@angular/core';\r\n\r\nclass C\u003cT extends number> {}\r\n\r\nconst c1 = inject(C); // type: C\u003cany>\r\nconst c2 = inject(C\u003c1>) // type: C\u003cany>\r\n\r\nabstract class AC\u003cT extends string> {}\r\n\r\nconst ac1 = inject(AC); // type: AC\u003cany>\r\nconst ac2 = inject(AC\u003c'ng'>); // type: AC\u003cany>\r\n```\r\n\r\nTyping issue is due to the AbstractType interface. It's currently defined as follows:\r\n\r\n```ts\r\ninterface AbstractType\u003cT> extends Function {\r\n prototype: T;\r\n}\r\n```\r\n\r\nTo solve this typing bug, `AbstractType` can be defined as follows:\r\n\r\n```ts\r\ntype AbstractType\u003cT> = abstract new (...args: unknown[]) => T;\r\n```\r\n\r\nHere is the [Stackblitz playground](https://stackblitz.com/edit/angular-qldklj-4f7htw?file=src%2Fapp%2Finject-typing-demo.ts) containing the `typedInject` function that solves this issue.\r\n\r\n---\r\n\r\n**This fix would introduce a breaking change.**\r\n\r\n---\r\n\r\n**If accepted, I'll create a PR for this bug.**\r\n\r\n### Please provide a link to a minimal reproduction of the bug\r\n\r\nhttps://stackblitz.com/edit/angular-qldklj?file=src%2Fapp%2Finject-typing-demo.ts\r\n\r\n### Please provide the exception or error you saw\r\n\r\n_No response_\r\n\r\n### Please provide the environment you discovered this bug in (run `ng version`)\r\n\r\n```true\r\nAngular CLI: 15.0.0\r\n```\r\n\r\n\r\n### Anything else?\r\n\r\nAs a workaround, we can explicitly set the return type:\r\n\r\n```ts\r\nclass C\u003cT extends number> {}\r\n\r\nconst c = inject\u003cC\u003cnumber>>(C);\r\n```","author":{"url":"https://github.com/markostanimirovic","@type":"Person","name":"markostanimirovic"},"datePublished":"2022-11-19T02:19:29Z","url":"https://github.com/angular/angular/issues/48126"}
| 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:384ff597-313d-5c10-6d4f-84c80f5b8ed6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D0FC:3DA8B4:7AA947:A4BDD9:6B06329F |
| html-safe-nonce | 4f2b8868ca47ded7438cdd088eb6293597e4763113d264b9629d963d4a03ac70 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMEZDOjNEQThCNDo3QUE5NDc6QTRCREQ5OjZCMDYzMjlGIiwidmlzaXRvcl9pZCI6IjMyMzgzMTk0NTIyMjM1ODI4NzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f1593e7ed5448073418a280e2ffc97a7a5a0d3cd9f52f689a1c072dc062a0a9e |
| hovercard-subject-tag | issue:1456125745 |
| 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/48126/issue_layout |
| twitter:image | https://opengraph.githubassets.com/984a0b49f179248a4e9476028cf4263b315ccb0b239c764fab2fea6903406be4/angular/angular/issues/48126 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/984a0b49f179248a4e9476028cf4263b315ccb0b239c764fab2fea6903406be4/angular/angular/issues/48126 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description If we use the inject function with a generic class (or abstract class), the return type won't be pro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markostanimirovic |
| hostname | github.com |
| expected-hostname | github.com |
| None | c3584c853cd39700ecff6e2cf8364295ffdb5313e571046c41cd2479270f7df0 |
| 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 | 9eb3bb1597d09170a5f282cd38ae376dee1a5ec1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width