Title: typeof foo === "array" causes type narrowing · Issue #13771 · microsoft/TypeScript · GitHub
Open Graph Title: typeof foo === "array" causes type narrowing · Issue #13771 · microsoft/TypeScript
X Title: typeof foo === "array" causes type narrowing · Issue #13771 · microsoft/TypeScript
Description: TypeScript Version: 2.1.5 / nightly (2.2.0-dev.20170130) Code function mixedMessages(): string | Array
Open Graph Description: TypeScript Version: 2.1.5 / nightly (2.2.0-dev.20170130) Code function mixedMessages(): string | Array
X Description: TypeScript Version: 2.1.5 / nightly (2.2.0-dev.20170130) Code function mixedMessages(): string | Array<string> { return ["Our relationship is strong", "so we should see other p...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13771
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"typeof foo === \"array\" causes type narrowing","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:** 2.1.5 / nightly (2.2.0-dev.20170130)\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction mixedMessages(): string | Array\u003cstring> {\r\n return [\"Our relationship is strong\", \"so we should see other people.\"];\r\n}\r\n\r\nconst ourRelationship = mixedMessages();\r\n\r\nif (typeof ourRelationship === \"array\") {\r\n throw new Error(\"There can be only one.\");\r\n}\r\n\r\nconsole.log(ourRelationship.substring(0, 10));\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nCompilation should fail with a type error, because in the final line, `ourRelationship` has type `string | Array\u003cstring>`, and arrays do not have a `substring` method.\r\n\r\n**Actual behavior:**\r\n\r\nThe if-throw is misinterpreted as a type narrowing from `string | Array\u003cstring>` to `string`. Compilation succeeds, and the generated javascript produces the runtime type error `ourRelationship.substring is not a function`.\r\n\r\nI'm no pro at javascript, so maybe this is an allowance for some obscure browser/environment which handles `typeof` differently, but in all the environments I looked at, `typeof [] === \"object\"`.","author":{"url":"https://github.com/sciolizer","@type":"Person","name":"sciolizer"},"datePublished":"2017-01-31T00:41:09Z","url":"https://github.com/microsoft/TypeScript/issues/13771"}
| 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:dc781f46-7466-d839-a450-4bf111c2b1a4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9270:7DF65:1295FB6:18148B4:6B03E3FE |
| html-safe-nonce | a4c1ea1dac3fcd9559a978fde063792a7c84f54a2fb43d5ff42a1469ddc9d5fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjcwOjdERjY1OjEyOTVGQjY6MTgxNDhCNDo2QjAzRTNGRSIsInZpc2l0b3JfaWQiOiI1ODg5NTA3NjY0ODc4MzYzNjQ2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ecd473bd003d8e64492822498c4f857f784c8df47e19b6255a1f20ab62071fd1 |
| hovercard-subject-tag | issue:204173363 |
| 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/13771/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8baa03acf039f78861bc0867d38ce9dc4749cd03babd05694c6770e50a39169a/microsoft/TypeScript/issues/13771 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8baa03acf039f78861bc0867d38ce9dc4749cd03babd05694c6770e50a39169a/microsoft/TypeScript/issues/13771 |
| og:image:alt | TypeScript Version: 2.1.5 / nightly (2.2.0-dev.20170130) Code function mixedMessages(): string | Array |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sciolizer |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width