Title: Accept de-structured elements in type predicates · Issue #41173 · microsoft/TypeScript · GitHub
Open Graph Title: Accept de-structured elements in type predicates · Issue #41173 · microsoft/TypeScript
X Title: Accept de-structured elements in type predicates · Issue #41173 · microsoft/TypeScript
Description: Search Terms type predicate reference binding pattern type predicate cannot reference destructured Suggestion The possibility to use destructured parameters in type predicates. Use Cases Destructuring is heavily used in functional/reacti...
Open Graph Description: Search Terms type predicate reference binding pattern type predicate cannot reference destructured Suggestion The possibility to use destructured parameters in type predicates. Use Cases Destructur...
X Description: Search Terms type predicate reference binding pattern type predicate cannot reference destructured Suggestion The possibility to use destructured parameters in type predicates. Use Cases Destructur...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/41173
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Accept de-structured elements in type predicates","articleBody":"## Search Terms\r\n\r\n- type predicate\r\n- reference binding pattern\r\n- type predicate cannot reference\r\n- destructured\r\n\r\n## Suggestion\r\n\r\nThe possibility to use destructured parameters in type predicates.\r\n\r\n## Use Cases\r\n\r\nDestructuring is heavily used in functional/reactive programming, notably with rxjs where various contextual properties tend to be passed between each operator.\r\n\r\nHaving the ability to succinctly test for types would make the code more readable, e.g.:\r\n```typescript\r\ntype Example = {\r\n a: number;\r\n b: string | undefined;\r\n};\r\n\r\nconst example: Example = {\r\n a: 42,\r\n b: 'hello';\r\n};\r\n\r\nof(example).pipe(\r\n guard(({ b }): b is string => b !== undefined, 'b cannot be undefined'),\r\n tap({ b }) => { /* b is now a string rather than a string | undefined })\r\n);\r\n```\r\nRight now the alternative is\r\n```typescript\r\nof(example).pipe(\r\n guard((x): x is Omit\u003ctypeof x, 'b'> & { b: string } => x.b !== undefined, 'b cannot be undefined'),\r\n tap({ b }) => { /* b is now a string rather than a string | undefined })\r\n);\r\n```\r\nOr, without a predicate\r\n```typescript\r\nof(example).pipe(\r\n map(x => {\r\n if (x.b === undefined) {\r\n throw new Error();\r\n }\r\n\r\n return x;\r\n }),\r\n tap({ b }) => { /* b is now a string rather than a string | undefined })\r\n);\r\n```\r\n\r\n## Examples\r\n\r\n```typescript\r\nfunction assertSomething(\r\n { property }: T\r\n): property is AssertionHere {\r\n return true;\r\n}\r\n```\r\n\r\nThis would roughly translate to something like:\r\n\r\n```typescript\r\nfunction assertSomething(\r\n obj: T\r\n): obj is Omit\u003cT, 'property'> & { property: AssertionHere } {\r\n return true;\r\n}\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] 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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n","author":{"url":"https://github.com/rraziel","@type":"Person","name":"rraziel"},"datePublished":"2020-10-20T09:41:59Z","url":"https://github.com/microsoft/TypeScript/issues/41173"}
| 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:217e60c7-c844-ee80-0a6c-bff12d42dd5e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B566:95C88:AC7865:E61858:6B00EBCA |
| html-safe-nonce | 2fc6525a7a6bbbd83f7698e225e7767ec4d8781956ad8946ddf6af133a34565d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTY2Ojk1Qzg4OkFDNzg2NTpFNjE4NTg6NkIwMEVCQ0EiLCJ2aXNpdG9yX2lkIjoiMzMzNzU5MjEzMTI3OTIwNTg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 19d452ed4951e0510fb0b9930cf701df85b878ff5ad435d148ab34b4a9461110 |
| hovercard-subject-tag | issue:725402325 |
| 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/41173/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4fda2426860cacd2ba451613f0a636c6110670eec630e1912ac75e4426e3781d/microsoft/TypeScript/issues/41173 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4fda2426860cacd2ba451613f0a636c6110670eec630e1912ac75e4426e3781d/microsoft/TypeScript/issues/41173 |
| og:image:alt | Search Terms type predicate reference binding pattern type predicate cannot reference destructured Suggestion The possibility to use destructured parameters in type predicates. Use Cases Destructur... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rraziel |
| 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