Title: Inconsistent/broken narrowing of void · Issue #32809 · microsoft/TypeScript · GitHub
Open Graph Title: Inconsistent/broken narrowing of void · Issue #32809 · microsoft/TypeScript
X Title: Inconsistent/broken narrowing of void · Issue #32809 · microsoft/TypeScript
Description: TypeScript Version: 3.5.1 Search Terms: void typeof undefined narrow use before assign Code // with strictNullChecks const x2: void = undefined; const x3: void = undefined; if(typeof x2 === "undefined") { x2 // Error: Variable 'x2' is us...
Open Graph Description: TypeScript Version: 3.5.1 Search Terms: void typeof undefined narrow use before assign Code // with strictNullChecks const x2: void = undefined; const x3: void = undefined; if(typeof x2 === "undefi...
X Description: TypeScript Version: 3.5.1 Search Terms: void typeof undefined narrow use before assign Code // with strictNullChecks const x2: void = undefined; const x3: void = undefined; if(typeof x2 === "u...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/32809
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent/broken narrowing of void","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.5.1\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** void typeof undefined narrow use before assign\r\n\r\n**Code**\r\n\r\n```ts\r\n// with strictNullChecks\r\nconst x2: void = undefined;\r\nconst x3: void = undefined;\r\n\r\nif(typeof x2 === \"undefined\") {\r\n x2 // Error: Variable 'x2' is used before being assigned.\r\n}\r\n\r\nif(typeof x3 !== \"undefined\") {\r\n x3 // never. x3 should probably be void\r\n}\r\n```\r\n\r\n```ts\r\n// without strictNullChecks\r\nconst x2: void = undefined;\r\n\r\nif(typeof x2 === \"undefined\") {\r\n x2 // x2 remains void here, not undefined.\r\n}\r\n\r\nif(typeof x2 !== \"undefined\" && x2 !== null) {\r\n x2 // x2 remains void here, rather than never.\r\n}\r\n```\r\n\r\n**Expected behavior:** \r\n\r\nUsing `x2` inside the first conditional in the `strictNullChecks` example should not error, as the variable absolutely was assigned to before use (whatever its type should be).\r\n\r\nAlso, the semantics around narrowing `void` should be consistent when `strictNullChecks` is on and when it's off. And, based on some of the [comments I've seen about `void`'s purpose](https://github.com/microsoft/TypeScript/issues/20006#issuecomment-344469659), it seems like the semantics with strictNullChecks off are the right ones.\r\n\r\n**Actual behavior:** \r\n\r\nThe use of `x2` triggers an error, as mentioned. \r\n\r\nAnd narrowing `void` is a no-op when `strictNullChecks` is disabled (i.e., it always results in `void`), whereas narrowing `void` can produce `never ` when `strictNullChecks` is enabled.\r\n\r\n**Playground Link:** \r\nWith strictNullChecks: https://www.typescriptlang.org/play/index.html#code/PTAEHcEsBcAtQM7QE6QMbQHIFcA2uBhWAUzQGsEAoNAewDslQAPAJgC5QA3GyAE1AC8obHV7EAZpDrFeAbmr1GTAMwdufQcNESpM+ZUjiAFNACeAB2I1xzFoIFCARCLGTpvRwEpQAb0qgA21AQUABRZGQaZA4ANQBDVDiAI1xiUAByVnTQSARhBBlQJIkotOKpAHNQOIQESAr3ADpKAF9KA2MzS2tmZVAAQgdQZ203GS9ff0CVYLBpTmJkVqA\r\n\r\nWithout strictNullChecks: https://www.typescriptlang.org/play/index.html?strictNullChecks=false#code/PTAEHcEsBcAsHsCu1QGdoCdIGNoDlEAbQgYVgFNsBrVAKG3gDt1QAPAJgC5QA3eSACagAvKESMB5AGaRG5AQG5akKQApoATwAO5eFLbsRw0QCJxkmXIEmAlKADetUM4OgQrjOQC2AQ1mpefiEKTwAaUEZ4FHNpWXkAOloAX1plNU0dPVcAQmNQMwlYqxNQADJSnLzGIkI7RxdXdw5QT19-QMFQEPJwjB84cgxQOB9GCPIeQcSkoA","author":{"url":"https://github.com/ethanresnick","@type":"Person","name":"ethanresnick"},"datePublished":"2019-08-12T01:01:07Z","url":"https://github.com/microsoft/TypeScript/issues/32809"}
| 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:550a53f2-b4da-630a-5976-6d0f7eed580b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C110:B1B84:52CA0F:68C36B:6B010ECB |
| html-safe-nonce | d798d9048eff0275ac457cce478f7af9473cdfecc12effffe7b52622b3658f2e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTEwOkIxQjg0OjUyQ0EwRjo2OEMzNkI6NkIwMTBFQ0IiLCJ2aXNpdG9yX2lkIjoiNjc1Nzc4NzI4OTAyNTk0MjUxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0f39d53cfc371bb29138f03021a407b99730dfc5c44eb536ea558baf7688c95f |
| hovercard-subject-tag | issue:479435456 |
| 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/32809/issue_layout |
| twitter:image | https://opengraph.githubassets.com/11117b71fe4ed9bc84fa260ba5be855c9a976b724d6d85763454305e6a643a73/microsoft/TypeScript/issues/32809 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/11117b71fe4ed9bc84fa260ba5be855c9a976b724d6d85763454305e6a643a73/microsoft/TypeScript/issues/32809 |
| og:image:alt | TypeScript Version: 3.5.1 Search Terms: void typeof undefined narrow use before assign Code // with strictNullChecks const x2: void = undefined; const x3: void = undefined; if(typeof x2 === "undefi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ethanresnick |
| 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 | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width