Title: Issue a warning when generic type inference produces {} · Issue #360 · microsoft/TypeScript · GitHub
Open Graph Title: Issue a warning when generic type inference produces {} · Issue #360 · microsoft/TypeScript
X Title: Issue a warning when generic type inference produces {} · Issue #360 · microsoft/TypeScript
Description: Motivating example (one of many): function compare
Open Graph Description: Motivating example (one of many): function compare
X Description: Motivating example (one of many): function compare<T>(lhs: T, rhs: T): boolean { return lhs === rhs; } if(compare('1', 1)) { // Expected: Error -- I made 'compare' generic for...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/360
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Issue a warning when generic type inference produces {}","articleBody":"Motivating example (one of many):\n\n``` ts\nfunction compare\u003cT>(lhs: T, rhs: T): boolean {\n return lhs === rhs;\n}\n\nif(compare('1', 1)) { // Expected: Error -- I made 'compare' generic for a reason!\n /* ... */\n}\n```\n### Proposal\n\nWhen generic type inference performs its Best Common Type operation (4.12.2), it should be an error if this returns `{}` when `{}` was not one of the input types.\n\nThis is entirely consistent with other behavior already in the compiler:\n\n``` ts\nvar foo = bar ? '1' : 1; // Error, no BCT between '1' and 1\nfunction fn() { // Error, no BCT between '1' and 1\n if(foo) {\n return '1';\n } else {\n return 1;\n }\n}\n```\n### Open Questions\n\nFrom @KamyarNazeri -- should this apply when there are zero input types? e.g.:\n\n``` ts\nclass List\u003cT> {\n items: T[];\n}\n\nvar x = new List(); // Probably want an error here\n```\n\nThat seems desirable, but has some collateral damage:\n\n``` ts\nfunction foo\u003cT>(x: number, a?: T, b?: T) { }\nfoo(0); // Error, zero input types provided for inference\n```\n\nThis would come up slightly more often than naively expected because many .d.ts authors (mistakenly) create generic types which don't actually consume their type parameters. Perhaps this warning would effectively discourage them from doing so?\n","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2014-08-05T04:12:30Z","url":"https://github.com/microsoft/TypeScript/issues/360"}
| 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:6fb57f31-8a33-8577-31e5-d52f6638ead1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B03C:2D8388:18132F:218905:6B008396 |
| html-safe-nonce | e692135c037008366f1df31ae2e5d87320bc54aa11916b7a40291bf3acb1aa73 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDNDOjJEODM4ODoxODEzMkY6MjE4OTA1OjZCMDA4Mzk2IiwidmlzaXRvcl9pZCI6IjMwMjk2MTA1NTAxNzU3NjEzMDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9f198af4e0d101cd748e8f2c2eb5ee28e3626252c796cb5ec58240026a30c3d5 |
| hovercard-subject-tag | issue:39483759 |
| 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/360/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8d729da65c6a25fbdeeddb45b02356b3cb35c3c8b2202d0ecf2041b042ecdf95/microsoft/TypeScript/issues/360 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8d729da65c6a25fbdeeddb45b02356b3cb35c3c8b2202d0ecf2041b042ecdf95/microsoft/TypeScript/issues/360 |
| og:image:alt | Motivating example (one of many): function compare |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4d3d8e50f02c1d7bf86d5acfa1d3bfe9366abd6528a6b429eb07a168429d2b61 |
| 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 | db758f8e02b8257935fb03edf81a3e76eb3abd11 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width