Title: Boolean() breaks strict null check · Issue #30367 · microsoft/TypeScript · GitHub
Open Graph Title: Boolean() breaks strict null check · Issue #30367 · microsoft/TypeScript
X Title: Boolean() breaks strict null check · Issue #30367 · microsoft/TypeScript
Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Boolean strictNullChecks Code Pre-requirements: you need to have strictNullChecks enabled; export interface Price { price?: { value: number }; } const a: Price = {}; if (a.price) { con...
Open Graph Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Boolean strictNullChecks Code Pre-requirements: you need to have strictNullChecks enabled; export interface Price { price?: { value: number }; }...
X Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Boolean strictNullChecks Code Pre-requirements: you need to have strictNullChecks enabled; export interface Price { price?: { value: number }; }...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/30367
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Boolean() breaks strict null check","articleBody":"\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.4.0-dev.201xxxxx\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\nBoolean strictNullChecks\r\n\r\n**Code**\r\nPre-requirements: you need to have `strictNullChecks` enabled;\r\n\r\n```ts\r\nexport interface Price {\r\n price?: {\r\n value: number\r\n };\r\n}\r\n\r\nconst a: Price = {};\r\n\r\nif (a.price) {\r\n console.log(a.price.value); // works as expected\r\n}\r\n\r\nif (Boolean(a.price)) {\r\n console.log(a.price.value); // throws an error for a.price, Object is possible undefined\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nI would be expecting this check also works.\r\n\r\n**Actual behavior:**\r\nActually, it brings an error `Object is possible undefined`.\r\n\r\nI think it's caused by the fact the `Boolean()` internally convert its argument to `any`, so TS lost check for real value for type `Price`. The same behavior can be achieved with `if (a.price as any)`.\r\nIt would be nice to have it fixed, but I'm not sure is it really possible.\r\n\r\n**Playground Link:** https://www.typescriptlang.org/play/index.html#src=export%20interface%20Price%20%7B%0D%0A%20%20price%3F%3A%20%7B%0D%0A%20%20%20%20value%3A%20number%0D%0A%20%20%7D%3B%0D%0A%7D%0D%0A%0D%0Aconst%20a%3A%20Price%20%3D%20%7B%7D%3B%0D%0A%0D%0Aif%20(Boolean(a.price)%20%7B%0D%0A%20%20console.log(a.price.value)%3B%20%20%2F%2F%20a.price%20Object%20is%20possible%20undefined%0D%0A%7D%0D%0A\r\n\r\n","author":{"url":"https://github.com/fetis","@type":"Person","name":"fetis"},"datePublished":"2019-03-13T15:18:37Z","url":"https://github.com/microsoft/TypeScript/issues/30367"}
| 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:4a49fb14-a5e2-03d1-0b09-a6566fbb1658 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B95A:62283:A96600:E24522:6B00EB37 |
| html-safe-nonce | fba01af1ad962edc8b6739f061aa03ad37a86623ac3ff7700f849a6c7a8febd3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTVBOjYyMjgzOkE5NjYwMDpFMjQ1MjI6NkIwMEVCMzciLCJ2aXNpdG9yX2lkIjoiODE2NzE2NDY4NDkyMzg5MDQ4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a6307d0d62cf43101eb3d048707f7d94096242880b870773ee3b10aa7062ddb4 |
| hovercard-subject-tag | issue:420558749 |
| 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/30367/issue_layout |
| twitter:image | https://opengraph.githubassets.com/74ef86f36cdf242c02f1e57725f6f9c546bb990b94a96af55c964f904ee76b05/microsoft/TypeScript/issues/30367 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/74ef86f36cdf242c02f1e57725f6f9c546bb990b94a96af55c964f904ee76b05/microsoft/TypeScript/issues/30367 |
| og:image:alt | TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: Boolean strictNullChecks Code Pre-requirements: you need to have strictNullChecks enabled; export interface Price { price?: { value: number }; }... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fetis |
| 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