Title: Assertion functions cannot be used to narrow discriminated union types · Issue #37241 · microsoft/TypeScript · GitHub
Open Graph Title: Assertion functions cannot be used to narrow discriminated union types · Issue #37241 · microsoft/TypeScript
X Title: Assertion functions cannot be used to narrow discriminated union types · Issue #37241 · microsoft/TypeScript
Description: Recently I tried to get creative with an assert function, but ran into a shortcoming. I think I understand why this is the behavior, but it would be nice if it wasn't the case. TypeScript Version: 3.9.0-dev.20200304 Search Terms: asserti...
Open Graph Description: Recently I tried to get creative with an assert function, but ran into a shortcoming. I think I understand why this is the behavior, but it would be nice if it wasn't the case. TypeScript Version: ...
X Description: Recently I tried to get creative with an assert function, but ran into a shortcoming. I think I understand why this is the behavior, but it would be nice if it wasn't the case. TypeScript Versi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/37241
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assertion functions cannot be used to narrow discriminated union types","articleBody":"Recently I tried to get creative with an assert function, but ran into a shortcoming. I think I understand *why* this is the behavior, but it would be nice if it wasn't the case.\r\n\r\n**TypeScript Version:** 3.9.0-dev.20200304\r\n\r\n**Search Terms:** assertion assert function discriminated union narrowing\r\n\r\n**Code**\r\n\r\n```ts\r\ninterface Cat {\r\n type: 'cat';\r\n canMeow: true;\r\n}\r\n\r\ninterface Dog {\r\n type: 'dog';\r\n canBark: true;\r\n}\r\n\r\ntype Animal = Cat | Dog;\r\n\r\nfunction assertEqual\u003cT>(value: any, type: T): asserts value is T {\r\n if (value !== type) {\r\n throw new Error('Not equal!');\r\n }\r\n}\r\n\r\nconst animal = { type: 'cat', canMeow: true } as Animal;\r\nassertEqual(animal.type, 'cat' as const);\r\n\r\n// Error\r\nanimal.type; // \"cat\"\r\nanimal.canMeow; // Property 'canMeow' does not exist on type 'Dog'\r\n\r\n// Works\r\nif (animal.type !== 'cat') {\r\n throw new Error();\r\n}\r\nanimal.canMeow; // true\r\n\r\n```\r\n\r\n**Expected behavior:**\r\nCalling `assertEqual(animal.type, 'cat')` would infer `animal` as a `Cat`.\r\n\r\n**Actual behavior:**\r\nOnly `animal.type` by itself is inferred; the type narrow does not \"propagate\" up to the union type.\r\n\r\n**Playground Link:** [Provided](https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=32&pc=1#code/JYOwLgpgTgZghgYwgAgMJzMg3gWAFDLJgCeADhAFzIDkCG1A3PoXSALIQD2A7lWFAFcITPAF98+UJFiIUAEU4BzbMyJlKNACZLGq1gCE4UANZ9Bw-OLz4S5ZAEEQwALZwANsgC8aDMgA+yAqKIvgwAiAIYMCcIMhwAM7x0GAAogCOAu4APAAqAHwAFABu7kJUcCDEADRq5FQ5AJTlicnxyCVuQsjAbTkqBN0wyMWlKACEnt62EA39hIRgABZQPMggENzIKVArUAXUAHKcmBAZ7mPUDSKEVlb4CDHxmBUu7l7YtRq09DWsHDxmLqiOJtRyvNwiBJJKCpM5uAovVxuAB00xq3zA1BByAeICeVwkeAA9EStjtOFB8Ij3Kj1AxkCTkAAiOhgJlUpxI5F-LjcemMgAKK3IMOINB5PCx2ggbRAx2QEAAHj1MDFPjQgtRCYyAOoU4zxSRDBGcmnTZATbwYy5zIjLVbrTbbXYFAliDng7kVf58hmk-hCQlAA)\r\n\r\n**Related Issues:** #11787 (most closely related, but that's for type guards, not the new assertion functions), #34596","author":{"url":"https://github.com/vaskevich","@type":"Person","name":"vaskevich"},"datePublished":"2020-03-06T01:47:33Z","url":"https://github.com/microsoft/TypeScript/issues/37241"}
| 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:800e9f22-08d5-2f34-1c42-a06e1853c986 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9902:1F2D:32A078:3FF758:6B010202 |
| html-safe-nonce | 337f08f0df643f4a5e10da47d1819cb17466901b8826ee0e0ba739b6297a1d2c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTAyOjFGMkQ6MzJBMDc4OjNGRjc1ODo2QjAxMDIwMiIsInZpc2l0b3JfaWQiOiI1MzYwNTM3NzE4ODgxMzIxNDc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8433148017ce2742b863583552bd62037e908f2e01d3f2efbc0a460404db4505 |
| hovercard-subject-tag | issue:576643359 |
| 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/37241/issue_layout |
| twitter:image | https://opengraph.githubassets.com/26c08bc4408a59093e7dfa2948e9501427845144a4aaea5eaf5dd0b8cbf02b97/microsoft/TypeScript/issues/37241 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/26c08bc4408a59093e7dfa2948e9501427845144a4aaea5eaf5dd0b8cbf02b97/microsoft/TypeScript/issues/37241 |
| og:image:alt | Recently I tried to get creative with an assert function, but ran into a shortcoming. I think I understand why this is the behavior, but it would be nice if it wasn't the case. TypeScript Version: ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vaskevich |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width