Title: Unions of assertion functions do not act as assertion functions · Issue #59707 · microsoft/TypeScript · GitHub
Open Graph Title: Unions of assertion functions do not act as assertion functions · Issue #59707 · microsoft/TypeScript
X Title: Unions of assertion functions do not act as assertion functions · Issue #59707 · microsoft/TypeScript
Description: 🔎 Search Terms narrowing, assertion function 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything ⏯ Playground Link https://tsplay.dev/Wv3DYw 💻 Code class b...
Open Graph Description: 🔎 Search Terms narrowing, assertion function 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything ⏯ Playground Link ...
X Description: 🔎 Search Terms narrowing, assertion function 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything ⏯ Playground L...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/59707
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unions of assertion functions do not act as assertion functions","articleBody":"### 🔎 Search Terms\n\nnarrowing, assertion function\n\n### 🕗 Version & Regression Information\n\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything\r\n\n\n### ⏯ Playground Link\n\nhttps://tsplay.dev/Wv3DYw\n\n### 💻 Code\n\n```ts\r\n class box\u003cT> {\r\n constructor(public value: T){}\r\n\r\n check(): this is box\u003cstring> {\r\n return typeof this.value == 'string';\r\n }\r\n\r\n assert(): asserts this is box\u003cstring> {\r\n if (typeof this.value != 'string') throw new Error();\r\n }\r\n\r\n private test() {\r\n this.assert();\r\n // type correctly narrowed\r\n this.value.substring(0);\r\n }\r\n }\r\n\r\n function make() : box\u003cstring> | box\u003cnumber> {\r\n return new box('a');\r\n }\r\n\r\n function assert(b: box\u003cstring> | box\u003cnumber>): asserts b is box\u003cstring> {\r\n if (typeof b.value != 'string') throw new Error();\r\n }\r\n\r\n const b = make();\r\n\r\n if (b.check()) {\r\n // type correctly narrowed\r\n b.value.substring(0);\r\n }\r\n\r\n b.assert();\r\n\r\n // type not narrowed (substring does not exist on type 'string | number')\r\n b.value.substring(0);\r\n \r\n assert(b);\r\n\r\n // type correctly narrowed\r\n b.value.substring(0);\r\n```\r\n\n\n### 🙁 Actual behavior\n\nType is not narrowed after method call\n\n### 🙂 Expected behavior\n\nI expected the type to be narrowed after method call since it is narrowed within other methods or when using a function external to the type.\n\n### Additional information about the issue\n\nhttps://stackoverflow.com/questions/78879014/typescript-type-assertion-does-not-narrow-class-instance-from-union","author":{"url":"https://github.com/AlexPaven","@type":"Person","name":"AlexPaven"},"datePublished":"2024-08-21T08:58:15Z","url":"https://github.com/microsoft/TypeScript/issues/59707"}
| 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:538cbbea-02fb-7b79-edf6-e03a2ec95ae4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE84:E75B8:5E136:751ED:6B00F21A |
| html-safe-nonce | d96dde134f23cbcf8d955be917f7fc2ceecf2e3c28c9cc03f09ae9734cff2a8c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTg0OkU3NUI4OjVFMTM2Ojc1MUVEOjZCMDBGMjFBIiwidmlzaXRvcl9pZCI6IjIwMDk2NzU1NzgyNDM4NzEyNTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5f2c60f73ed4b0e74cf0434f75d3b03d3b5755eb79df894831f5e9a1800410ff |
| hovercard-subject-tag | issue:2477480260 |
| 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/59707/issue_layout |
| twitter:image | https://opengraph.githubassets.com/80c536ed4a5124dbbe6b4b26602ef16548035b5001085247ecec1f2c1705cde3/microsoft/TypeScript/issues/59707 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/80c536ed4a5124dbbe6b4b26602ef16548035b5001085247ecec1f2c1705cde3/microsoft/TypeScript/issues/59707 |
| og:image:alt | 🔎 Search Terms narrowing, assertion function 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything ⏯ Playground Link ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexPaven |
| 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