Title: Generic parameter of function is inferred to be {} when argument of conditional type is passed in · Issue #25301 · microsoft/TypeScript · GitHub
Open Graph Title: Generic parameter of function is inferred to be {} when argument of conditional type is passed in · Issue #25301 · microsoft/TypeScript
X Title: Generic parameter of function is inferred to be {} when argument of conditional type is passed in · Issue #25301 · microsoft/TypeScript
Description: TypeScript Version: 2.8.1, 2.9.2, 3.0.0-dev.20180628 Search Terms: conditional type inference Code // very contrived code to demonstrate the issue // this conditional type trips type inference type Action
= P extends void ? { type: st...
Open Graph Description: TypeScript Version: 2.8.1, 2.9.2, 3.0.0-dev.20180628 Search Terms: conditional type inference Code // very contrived code to demonstrate the issue // this conditional type trips type inference type...
X Description: TypeScript Version: 2.8.1, 2.9.2, 3.0.0-dev.20180628 Search Terms: conditional type inference Code // very contrived code to demonstrate the issue // this conditional type trips type inference type...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/25301
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic parameter of function is inferred to be {} when argument of conditional type is passed in","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** \r\n2.8.1, 2.9.2, 3.0.0-dev.20180628\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** \r\nconditional type inference\r\n\r\n**Code**\r\n\r\n```ts\r\n// very contrived code to demonstrate the issue\r\n\r\n// this conditional type trips type inference\r\ntype Action\u003cP> = P extends void\r\n ? { type: string }\r\n : { type: string, payload: P }\r\n\r\ninterface Dispatch {\r\n // this conditional type seems to have no negative impact on type inference\r\n \u003cP>(action: Action\u003cP>): P extends void ? undefined : P\r\n}\r\n\r\ndeclare const dispatch: Dispatch\r\n\r\nconst numberAction: Action\u003cnumber> = {\r\n type: 'dummy',\r\n payload: 0,\r\n }\r\n // infered result type is {}, expected one is number\r\n , numberResult: number = dispatch(numberAction)\r\n\r\nconst voidAction: Action\u003cvoid> = {\r\n type: 'dummy',\r\n }\r\n // seems infered P is {}, so it is not allowed to pass voidAction to dispatch\r\n , voidResult: undefined = dispatch(voidAction)\r\n\r\n```\r\n\r\n**Expected behavior:**\r\nType checking succeedes\r\n\r\n**Actual behavior:**\r\nType checking fails for two places:\r\n- numberResult: number = dispatch(numberAction)\r\n- voidResult: undefined = dispatch(voidAction)\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior --> \r\n[Playground](https://www.typescriptlang.org/play/index.html#src=%2F%2F%20very%20contrived%20code%20to%20demonstrate%20issue%0D%0A%0D%0A%2F%2F%20this%20conditional%20type%20trips%20type%20inference%0D%0Atype%20Action%3CP%3E%20%3D%20P%20extends%20void%0D%0A%20%20%20%20%3F%20%7B%20type%3A%20string%20%7D%0D%0A%20%20%20%20%3A%20%7B%20type%3A%20string%2C%20payload%3A%20P%20%7D%0D%0A%0D%0Ainterface%20Dispatch%20%7B%0D%0A%20%20%20%20%2F%2F%20this%20conditional%20type%20seems%20to%20have%20no%20negative%20impact%20on%20type%20inference%0D%0A%20%20%20%20%3CP%3E(action%3A%20Action%3CP%3E)%3A%20P%20extends%20void%20%3F%20undefined%20%3A%20P%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20dispatch%3A%20Dispatch%0D%0A%0D%0Aconst%20numberAction%3A%20Action%3Cnumber%3E%20%3D%20%7B%0D%0A%20%20%20%20%20%20%20%20type%3A%20'dummy'%2C%0D%0A%20%20%20%20%20%20%20%20payload%3A%200%2C%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%2F%2F%20infered%20result%20type%20is%20%7B%7D%2C%20expected%20one%20is%20number%0D%0A%20%20%20%20%2C%20numberResult%3A%20number%20%3D%20dispatch(numberAction)%0D%0A%0D%0Aconst%20voidAction%3A%20Action%3Cvoid%3E%20%3D%20%7B%0D%0A%20%20%20%20%20%20%20%20type%3A%20'dummy'%2C%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%2F%2F%20seems%20infered%20P%20is%20%7B%7D%2C%20so%20it%20is%20not%20allowed%20to%20pass%20voidAction%20to%20dispatch%0D%0A%20%20%20%20%2C%20voidResult%3A%20undefined%20%3D%20dispatch(voidAction)%0D%0A)\r\n\r\nI believe this sample captures the issue but just in case there is a more verbose [sample with original types](https://www.typescriptlang.org/play/index.html#src=type%20Default%20%3D%20'some%20unique%20type'%0D%0A%0D%0A%2F%2F%20%40param%20P%20Payload%20type%0D%0A%2F%2F%20%40param%20R%20Result%20type%20of%20%22dispatch%22%20function%20when%20action%20of%20this%20type%20is%20dispatched.%0D%0A%2F%2F%20By%20default%20it%20should%20be%20action%20itself%20but%20I%20see%20no%20way%20to%20declare%20such%20recursive%20type%3A%0D%0A%2F%2F%20ActionType%3CP%2C%20R%20%3D%0D%0A%2F%2F%20%20%20%20ActionType%3CP%2C%20R%20%3D%0D%0A%2F%2F%20%20%20%20%20%20%20%20...%0D%0A%2F%2F%20%20%20%20%3E%0D%0A%2F%2F%20%3E%0D%0A%2F%2F%20So%20'Default'%20terminator%20is%20used%0D%0Atype%20ActionType%3CP%2C%20R%20%3D%20Default%3E%20%3D%20string%20%26%20%7B%0D%0A%20%20%20%20attachPayloadTypeHack%3F%3A%20P%20%26%20never%0D%0A%20%20%20%20attachDispatchResultTypeHack%3F%3A%20R%20%26%20never%0D%0A%7D%0D%0A%0D%0Atype%20Action%3CP%2C%20R%3E%20%3D%20P%20extends%20void%0D%0A%20%20%20%20%3F%20%7B%20type%3A%20ActionType%3CP%2C%20R%3E%20%7D%0D%0A%20%20%20%20%3A%20%7B%20type%3A%20ActionType%3CP%2C%20R%3E%2C%20payload%3A%20P%20%7D%0D%0A%0D%0Atype%20DispatchResult%3CP%2C%20R%3E%20%3D%20R%20extends%20Default%20%3F%20Action%3CP%2C%20Default%3E%20%3A%20R%0D%0A%0D%0Ainterface%20Dispatch%20%7B%0D%0A%20%20%20%20%3CP%2C%20R%3E(action%3A%20Action%3CP%2C%20R%3E)%3A%20DispatchResult%3CP%2C%20R%3E%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20dispatch%3A%20Dispatch%0D%0A%0D%0Aconst%20action%3A%20Action%3Cnumber%2C%20string%3E%20%3D%20%7B%0D%0A%20%20%20%20%20%20%20%20type%3A%20'ACTION'%2C%0D%0A%20%20%20%20%20%20%20%20payload%3A%200%2C%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%2C%20result%3A%20string%20%3D%20dispatch(action)%0D%0A)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\nNot sure #25299 #24784","author":{"url":"https://github.com/andriy-kudrya","@type":"Person","name":"andriy-kudrya"},"datePublished":"2018-06-28T18:39:11Z","url":"https://github.com/microsoft/TypeScript/issues/25301"}
| 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:ae43a2d1-cb8f-dc1a-4e5b-f840f7f555ab |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81E0:36A3E:234D7D2:2F11F04:6B0197F9 |
| html-safe-nonce | 02811f33c41672704ae9267ff14b06e0e606565328ed2f6eb31352af00d2ac25 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUUwOjM2QTNFOjIzNEQ3RDI6MkYxMUYwNDo2QjAxOTdGOSIsInZpc2l0b3JfaWQiOiIzNzkyMTI2Mzk1MjI4OTg1MzM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5895fecb8235723554518aac8cad86df3e40338964b0dcb1a798fae62a42791f |
| hovercard-subject-tag | issue:336734611 |
| 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/25301/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d81ab06d779a71c057c029da867d91a62bced5c7444fd7741e613897db8a0b59/microsoft/TypeScript/issues/25301 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d81ab06d779a71c057c029da867d91a62bced5c7444fd7741e613897db8a0b59/microsoft/TypeScript/issues/25301 |
| og:image:alt | TypeScript Version: 2.8.1, 2.9.2, 3.0.0-dev.20180628 Search Terms: conditional type inference Code // very contrived code to demonstrate the issue // this conditional type trips type inference type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andriy-kudrya |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width