Title: Infer type guard from predicate for functions accepting type guards. · Issue #19468 · microsoft/TypeScript · GitHub
Open Graph Title: Infer type guard from predicate for functions accepting type guards. · Issue #19468 · microsoft/TypeScript
X Title: Infer type guard from predicate for functions accepting type guards. · Issue #19468 · microsoft/TypeScript
Description: It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument. Consider functions like Array.filter or Observable.filter being used to filter a union type with discr...
Open Graph Description: It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument. Consider functions like Array.filter or Observable.filter bein...
X Description: It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument. Consider functions like Array.filter or Observable.filter bein...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/19468
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Infer type guard from predicate for functions accepting type guards.","articleBody":"It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument.\r\n\r\nConsider functions like `Array.filter` or `Observable.filter` being used to filter a union type with discriminant:\r\n\r\n type FooBar = { type: 'foo' } | { type: 'bar' };\r\n const isFoo = \u003c(fb: FooBar) => fb is { type: 'foo'}>(fb => fb.type == \"foo\");\r\n const fooBar$: Observable\u003cFooBar> = { /* ... */ };\r\n const foo$ = fooBar$.filter(isFoo); //We can use the type guard 'isFoo' to narrow the type to Foo.\r\n\r\nHowever this is quite a lot of wiring for something that anywhere else is quite straight forward. In an if/else or switch block, a type guard isn't necessary... Typescript infers the type from the predicate:\r\n\r\n if(fb.type === 'foo') {\r\n //'fb' is { type: 'foo' }\r\n }\r\n\r\nHaving to declare the type guard also requires us to 'repeat' out the shape of the type which can be quite painful if it isn't a type alias or class... i.e. we have to explicitly write `fb is { type: 'foo' }. \r\n\r\nTypescript should be able to infer that the correct type is `{ type: 'foo' }` from the supplied predicate.\r\n\r\nIn such an ideal world, we should be able to simply write:\r\n\r\n const foo$ = fooBar$.filter(fb => fb.type === 'foo');\r\n\r\nIf Typescript requires us to explicitly opt-in the function as a type-guard, then it should be possible to do so without explicitly repeating the type in the declaration. Maybe something like:\r\n\r\n const foo$ = fooBar$.filter(\u003cfb is>(fb => fb.type === 'foo'));","author":{"url":"https://github.com/martaver","@type":"Person","name":"martaver"},"datePublished":"2017-10-25T12:01:21Z","url":"https://github.com/microsoft/TypeScript/issues/19468"}
| 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:c7f219d0-3025-c9a2-c5aa-b6c52c6fc6e1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E7A6:1AE20C:B14A8E:E1870A:6B00F5C1 |
| html-safe-nonce | ff9d9c8571875b959c8b0eef863f18215e21293cff16d1b175bc68a064225520 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFN0E2OjFBRTIwQzpCMTRBOEU6RTE4NzBBOjZCMDBGNUMxIiwidmlzaXRvcl9pZCI6IjY1MjgxMjQ0NDUxNTQ5MzIxNjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bd5ecd61c418a90a65fca6ada007ef5cfc445b286c88a2d688483703f663b7c8 |
| hovercard-subject-tag | issue:268370761 |
| 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/19468/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b7fccf78ab5920f128d6382e3c9c9a00076c8d4fd79fc311408e4b8566a1614d/microsoft/TypeScript/issues/19468 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b7fccf78ab5920f128d6382e3c9c9a00076c8d4fd79fc311408e4b8566a1614d/microsoft/TypeScript/issues/19468 |
| og:image:alt | It would be great to avoid the ceremony of having to declare separate type guards for functions accepting a type guard as an argument. Consider functions like Array.filter or Observable.filter bein... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | martaver |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width