Title: Proposal: strict flag to prevent uninferrable generic types from being inferred as {} · Issue #27288 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: strict flag to prevent uninferrable generic types from being inferred as {} · Issue #27288 · microsoft/TypeScript
X Title: Proposal: strict flag to prevent uninferrable generic types from being inferred as {} · Issue #27288 · microsoft/TypeScript
Description: I'm sure there an issue or discussion about this I missed, but I couldn't find it via search. Search Terms Generic, infer, default, {}, { }, empty, shape, object Suggestion Behind a new strict mode flag --strictGenericBindings, when bind...
Open Graph Description: I'm sure there an issue or discussion about this I missed, but I couldn't find it via search. Search Terms Generic, infer, default, {}, { }, empty, shape, object Suggestion Behind a new strict mode...
X Description: I'm sure there an issue or discussion about this I missed, but I couldn't find it via search. Search Terms Generic, infer, default, {}, { }, empty, shape, object Suggestion Behind a new str...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27288
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: strict flag to prevent uninferrable generic types from being inferred as {}","articleBody":"I'm sure there an issue or discussion about this I missed, but I couldn't find it via search.\r\n\r\n## Search Terms\r\n\r\nGeneric, infer, default, {}, { }, empty, shape, object\r\n\r\n## Suggestion\r\n\r\nBehind a new `strict` mode flag `--strictGenericBindings`, when binding concrete types to generic type parameters, if a type can't be inferred TSC should throw an error at compile time instead of defaulting to `{}`.\r\n\r\n## Use Cases\r\n\r\nLet's say you have the following Promise:\r\n\r\n```ts\r\nconst p = new Promise(resolve =\u003e resolve(42)) // Promise\u003c{}\u003e\r\np.then(r =\u003e\r\n r + 4 // Error: Operator '+' cannot be applied to types '{}' and '4'.\r\n)\r\n```\r\n\r\nThis gives an error when you try to use the Promise's result. This experience isn't ideal, because the error is non-local. Looking at the code that threw that error, it's not obvious why `r` is `{}`.\r\n\r\nIf I try to fix this by adding an explicit annotate when I consume `r`, it works:\r\n\r\n```ts\r\np.then((r: number) =\u003e {\r\n r + 4 // OK\r\n})\r\n```\r\n\r\nBut if I have `strictFunctionTypes` enabled, that breaks because `{}` isn't assignable to `number`:\r\n\r\n```ts\r\np.then((r: number) =\u003e {\r\n r + 4 // Error: Argument of type '(r: number) =\u003e void' is not assignable to parameter of type '(value: {}) =\u003e void | PromiseLike\u003cvoid\u003e'.\r\n})\r\n```\r\n\r\nThe real fix is to explicitly bind a type parameter when I consume `Promise`:\r\n\r\n```ts\r\nconst p = new Promise\u003cnumber\u003e(resolve =\u003e resolve(42)) \r\np.then(r =\u003e {\r\n r + 4\r\n})\r\n```\r\n\r\nBut unfortunately, the error messages didn't help me get there.\r\n\r\nA possible solution to this problem is to throw a helpful error message when a generic type parameter can't be inferred. This can help prevent bugs, and help programmers catch bugs earlier:\r\n\r\n```ts\r\nconst p = new Promise(resolve =\u003e resolve(42)) // Error: Unable to infer a generic type for Promise\u003cT\u003e - please bind one explicitly. Eg. new Promise\u003cmyType\u003e(...).\r\np.then(r =\u003e {\r\n r + 4\r\n})\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [ ] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n## Related Issues\r\n\r\n- https://github.com/Microsoft/TypeScript/issues/26809\r\n- https://github.com/Microsoft/TypeScript/issues/10247\r\n","author":{"url":"https://github.com/bcherny","@type":"Person","name":"bcherny"},"datePublished":"2018-09-23T00:00:09.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/27288/TypeScript/issues/27288"}
| 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:8a71e1ff-fa70-a9ec-ec85-252f51da33e5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EAC6:307AD9:3A569A:53DDCE:6A61C9CE |
| html-safe-nonce | 16c46d4d6bb044c49bf2dcb495002d8ae8df5c416485c28c4241c2e0157255cf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQUM2OjMwN0FEOTozQTU2OUE6NTNERENFOjZBNjFDOUNFIiwidmlzaXRvcl9pZCI6IjI0MjQyNjA1Njc5Mjg3ODUzNTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 83691aa8d7ad8833847d6b49e9c658fa6fd633ef3051cc57689c0901cc18496c |
| hovercard-subject-tag | issue:362900433 |
| 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/27288/issue_layout |
| twitter:image | https://opengraph.githubassets.com/26359dc014fddbff0a4f971edfbc1a64f16944f7dfe27a1c187b150cc748d221/microsoft/TypeScript/issues/27288 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/26359dc014fddbff0a4f971edfbc1a64f16944f7dfe27a1c187b150cc748d221/microsoft/TypeScript/issues/27288 |
| og:image:alt | I'm sure there an issue or discussion about this I missed, but I couldn't find it via search. Search Terms Generic, infer, default, {}, { }, empty, shape, object Suggestion Behind a new strict mode... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bcherny |
| hostname | github.com |
| expected-hostname | github.com |
| None | b2de8c74e5e61e893155ba46ee41bc66170c1644cb795adefa8386d490f7781c |
| 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 | d1866027ded575df8a15c731dd8b9986c9483ceb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width