Title: Suggestion: Type narrowing also narrows conditional types · Issue #21879 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Type narrowing also narrows conditional types · Issue #21879 · microsoft/TypeScript
X Title: Suggestion: Type narrowing also narrows conditional types · Issue #21879 · microsoft/TypeScript
Description: TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used to ensure full case coverage interface Foo { kind: 'foo'; } declare function isFoo(foobar: Foo | Bar): foobar is Foo; interface Bar { k...
Open Graph Description: TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used to ensure full case coverage interface Foo { kind: 'foo'; } declare function isFoo(foobar: Foo ...
X Description: TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used to ensure full case coverage interface Foo { kind: 'foo'; } declare function isFoo(foob...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/21879
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Type narrowing also narrows conditional types","articleBody":"**TypeScript Version:** 2.8.0-dev.20180204\r\n\r\n**Code**\r\n\r\n```ts\r\ndeclare function broke(impossible: never): never; // used to ensure full case coverage\r\n\r\ninterface Foo { kind: 'foo'; }\r\ndeclare function isFoo(foobar: Foo | Bar): foobar is Foo;\r\ninterface Bar { kind: 'bar'; }\r\ndeclare function isBar(foobar: Foo | Bar): foobar is Bar;\r\n\r\nfunction mapFooOrBar\u003cFooBar extends Foo | Bar, R>(\r\n foobar: FooBar,\r\n mapFoo: FooBar extends Foo ? ((value: Foo) => R) : ((impossible: never) => never),\r\n mapBar: FooBar extends Bar ? ((value: Bar) => R) : ((impossible: never) => never),\r\n): R {\r\n if (isFoo(foobar)) {\r\n return mapFoo(foobar);\r\n/* ^^^^^^^^^^^^^^\r\nCannot invoke an expression whose type lacks a call signature. Type '((value: Foo) => R) | ((impossible: never) => never)' has no compatible call signatures. */\r\n }\r\n else if (isBar(foobar)) {\r\n return mapBar(foobar);\r\n/* ^^^^^^^^^^^^^^\r\nCannot invoke an expression whose type lacks a call signature. Type '((value: Bar) => R) | ((impossible: never) => never)' has no compatible call signatures. */\r\n }\r\n else {\r\n return broke(foobar as never);\r\n }\r\n}\r\n```\r\n(the casting `foobar as never` for the `broke` function is a workaround for #20375)\r\n\r\n**Expected Behavior**\r\nCompile without errors.\r\n\r\n**Actual Behavior**\r\nThe types of `mapFoo` and `mapBar` are not reconsidered in light of passing the typeguard:\r\n`Cannot invoke an expression whose type lacks a call signature. Type '((value: Foo) => R) | ((impossible: never) => never)' has no compatible call signatures.`\r\n\r\n`Cannot invoke an expression whose type lacks a call signature. Type '((value: Bar) => R) | ((impossible: never) => never)' has no compatible call signatures.`\r\n\r\nMy use case for this is a situation where I have a union of three types, but some cases will only ever use a union of two of those types. I want this kind of utility function to be re-usable for those situations, but indicate that the handlers for unincluded cases will never be called (and allow us to type-safely stub those branches, e.g. with that `broke` function).\r\n\r\nOur project uses these kinds of unions and utility functions *a lot* and this would significantly improve them.","author":{"url":"https://github.com/krryan","@type":"Person","name":"krryan"},"datePublished":"2018-02-12T14:22:12Z","url":"https://github.com/microsoft/TypeScript/issues/21879"}
| 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:41489216-8282-af3e-167f-eab7b387f3ea |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8600:F7C4C:248EF16:30F33D3:6B01A4E0 |
| html-safe-nonce | b4c80b80e9837ebd36f87107e0d1ac25b1d005da63f397f0567f08d1833c4b0c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjAwOkY3QzRDOjI0OEVGMTY6MzBGMzNEMzo2QjAxQTRFMCIsInZpc2l0b3JfaWQiOiI0MDIwNTY1NjI1NDE0OTg0OTI4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d808ad65b154a173e7eb9a924a8c1cbad1ffb681294b52f63cbe13bde54392c1 |
| hovercard-subject-tag | issue:296391029 |
| 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/21879/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d7e1289bc894932483bf9c5b1341092a7a93eeb315385d3f8f02d75226ee8653/microsoft/TypeScript/issues/21879 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d7e1289bc894932483bf9c5b1341092a7a93eeb315385d3f8f02d75226ee8653/microsoft/TypeScript/issues/21879 |
| og:image:alt | TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used to ensure full case coverage interface Foo { kind: 'foo'; } declare function isFoo(foobar: Foo ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | krryan |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width