Title: Suggestion: Improve generics inference with decorators · Issue #2686 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Improve generics inference with decorators · Issue #2686 · microsoft/TypeScript
X Title: Suggestion: Improve generics inference with decorators · Issue #2686 · microsoft/TypeScript
Description: There have been a lot of users who find it strange that this would compile: declare function forEach(array: U[], callback: (item: U) => void); var strings: string[]; var callback: (item: {}) => void; forEach(strings, callback); // U i...
Open Graph Description: There have been a lot of users who find it strange that this would compile: declare function forEach(array: U[], callback: (item: U) => void); var strings: string[]; var callback: (item: {}) => ...
X Description: There have been a lot of users who find it strange that this would compile: declare function forEach<U>(array: U[], callback: (item: U) => void); var strings: string[]; var callback: (item...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2686
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Improve generics inference with decorators","articleBody":"There have been a lot of users who find it strange that this would compile:\n\n``` typescript\ndeclare function forEach\u003cU>(array: U[], callback: (item: U) => void);\nvar strings: string[];\nvar callback: (item: {}) => void;\nforEach(strings, callback); // U is inferred as {}\n```\n\nSuggestion: use the decorator `@infer` to tell the compiler to infer a generic from that argument.\nExample:\n\n``` typescript\ndeclare function forEach\u003cU>(@infer\u003cU> array: U[], callback: (item: U) => void);\nvar strings: string[];\nvar callback: (item: {}) => void;\nforEach(strings, callback); // U is inferred as string (using only the decorated argument), thus this is an error\n```\n\nYou can also add `@infer` to multiple arguments, like this:\n\n``` typescript\ndeclare function forEachOnTwoArrays\u003cU>(@infer\u003cU> firstArray: U[], @infer\u003cU> secondArray: U[], callback: (item: U) => void);\n```\n\nAdding `@infer` to all parameters would have the same behavior as having no `@infer`. Adding `@infer` is optional, thus not a breaking change.\n\nRules for `@infer`:\n- You may only add type parameters of the direct containing function.\n- You can add multiple type parameters like this: `@infer\u003cT> @infer\u003cU>` (or maybe `@infer\u003cT, U>`?)\n- Generics that are not referenced from an `@infer` decorator are inferred using the current behavior\n- Every generic `T` that is referenced from an `@infer\u003cT>` decorator is resolved using all parameters that are decorated with `@infer\u003cT>` of that function.\n\nNote that this syntax is currently not supported, as the compiler expects `(` after the generics. An alternative would be to write it as `@infer\u003cU>()`, but I think `@infer\u003cU>` is cleaner.\n","author":{"url":"https://github.com/ivogabe","@type":"Person","name":"ivogabe"},"datePublished":"2015-04-09T08:42:41Z","url":"https://github.com/microsoft/TypeScript/issues/2686"}
| 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:1e9fe128-af43-7e28-a2b1-a8862601328e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF8C:ADA9D:8AD3F2:B6063E:6B00DD0E |
| html-safe-nonce | 9565d1ac8fd1c12ef7ddb76f69e49ab488a6d49cc60a6244a0eb5d4acb9e9ba5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjhDOkFEQTlEOjhBRDNGMjpCNjA2M0U6NkIwMEREMEUiLCJ2aXNpdG9yX2lkIjoiMzA2MDI2NDk5NDQ4NzcyMTIzMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1507a81f8f455606d6a9085780356f41f5e0757ada387a6017d1ffb2f2ee3675 |
| hovercard-subject-tag | issue:67310164 |
| 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/microsoft/TypeScript/2686/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b23018d7f1472baf9729ba3831be80e15aa2eb67c9e9a1a4eeb7655acdca25a/microsoft/TypeScript/issues/2686 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b23018d7f1472baf9729ba3831be80e15aa2eb67c9e9a1a4eeb7655acdca25a/microsoft/TypeScript/issues/2686 |
| og:image:alt | There have been a lot of users who find it strange that this would compile: declare function forEach(array: U[], callback: (item: U) => void); var strings: string[]; var callback: (item: {}) => ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ivogabe |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width