Title: Type inference for conditional callback parameters breaks down · Issue #22149 · microsoft/TypeScript · GitHub
Open Graph Title: Type inference for conditional callback parameters breaks down · Issue #22149 · microsoft/TypeScript
X Title: Type inference for conditional callback parameters breaks down · Issue #22149 · microsoft/TypeScript
Description: This is based on #21879; I have a work-around for that issue but still having a separate issue. TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used to ensure full case coverage interface ...
Open Graph Description: This is based on #21879; I have a work-around for that issue but still having a separate issue. TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used...
X Description: This is based on #21879; I have a work-around for that issue but still having a separate issue. TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22149
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type inference for conditional callback parameters breaks down","articleBody":"This is based on #21879; I have a work-around for that issue but still having a separate issue.\r\n\r\n**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 // workaround for #21879\r\n const handle = mapFoo as (value: Foo) => R;\r\n return handle(foobar);\r\n }\r\n else if (isBar(foobar)) {\r\n // workaround for #21879\r\n const handle = mapBar as (value: Bar) => R;\r\n return handle(foobar);\r\n }\r\n else {\r\n // workaround for #20375\r\n return broke(foobar as never);\r\n }\r\n}\r\n\r\ndeclare function toFoo(): Foo;\r\n\r\nmapFooOrBar(\r\n toFoo(),\r\n foo => foo.kind,\r\n/* ^^^\r\nParameter 'foo' implicitly has an 'any' type. */\r\n bar => broke(bar)\r\n/* ^^^^^^^^^^^^^^^^^\r\n ^^^\r\nArgument of type '(bar: any) => any' is not assignable to parameter of type '(impossible: never) => never'.\r\n Type 'any' is not assignable to type 'never'. */\r\n);\r\n\r\nmapFooOrBar\u003cFoo, string>(\r\n toFoo(),\r\n foo => foo.kind,\r\n bar => broke(bar)\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\nTypescript fails to infer the type of `foo` or `bar` in the callbacks, despite correctly inferring the type of the generic `FooBar` parameter in `mapFooOrBar`. Hovering over the first invocation of `mapFooOrBar` in VS Code, I see\r\n```ts\r\nfunction mapFooOrBar\u003cFoo, {}>(foobar: Foo, mapFoo: (value: Foo) => {}, mapBar: (impossible: never) => never): {}\r\n```\r\nWhich all looks correct, excepting the `{}` instead of `string` or `'foo'` as the second parameter. But despite getting all that right, the parameters in the callback are inferred as `any`, which is likely the source of the `{}` second parameter. (Eliminating the second parameter and making the callbacks return `void` does not improve matters.)","author":{"url":"https://github.com/krryan","@type":"Person","name":"krryan"},"datePublished":"2018-02-23T17:22:25Z","url":"https://github.com/microsoft/TypeScript/issues/22149"}
| 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:6865ee4b-2014-dae0-a00f-f9c587deb11f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B654:669A1:4B7BC1:63E0BB:6B019B7A |
| html-safe-nonce | 40fe6cefe471793d8589dcad47de1e14e67cb87c2b1c2ca6b270c7fe92b21333 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNjU0OjY2OUExOjRCN0JDMTo2M0UwQkI6NkIwMTlCN0EiLCJ2aXNpdG9yX2lkIjoiNjY4NjU5MDk0NDMyNzg2NzI1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a67b9539d76845c75c6d7008e0eaeb0bda2c7bf4f9be36fb5007bfb41c5d3467 |
| hovercard-subject-tag | issue:299787596 |
| 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/22149/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7aa80f901a2e3858dc1dab7f357e07d9bd1958eb72cb7259d56ea50f5e97e410/microsoft/TypeScript/issues/22149 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7aa80f901a2e3858dc1dab7f357e07d9bd1958eb72cb7259d56ea50f5e97e410/microsoft/TypeScript/issues/22149 |
| og:image:alt | This is based on #21879; I have a work-around for that issue but still having a separate issue. TypeScript Version: 2.8.0-dev.20180204 Code declare function broke(impossible: never): never; // used... |
| 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