Title: Unsoundness in coercion from object to boolean · Issue #31464 · microsoft/TypeScript · GitHub
Open Graph Title: Unsoundness in coercion from object to boolean · Issue #31464 · microsoft/TypeScript
X Title: Unsoundness in coercion from object to boolean · Issue #31464 · microsoft/TypeScript
Description: TypeScript Version: 3.4.3, typescript@3.5.0-dev.20190518 Search Terms: unsound coercion object boolean Code const obj: { toFixed: unknown } = 0; // val has type "123" const val = obj && "123"; console.log(val.slice()); // <- runtime erro...
Open Graph Description: TypeScript Version: 3.4.3, typescript@3.5.0-dev.20190518 Search Terms: unsound coercion object boolean Code const obj: { toFixed: unknown } = 0; // val has type "123" const val = obj && "123"; cons...
X Description: TypeScript Version: 3.4.3, typescript@3.5.0-dev.20190518 Search Terms: unsound coercion object boolean Code const obj: { toFixed: unknown } = 0; // val has type "123" const val = obj &...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31464
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unsoundness in coercion from object to boolean","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.4.3, typescript@3.5.0-dev.20190518\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** unsound coercion object boolean\r\n\r\n**Code**\r\n\r\n```ts\r\nconst obj: { toFixed: unknown } = 0;\r\n// val has type \"123\"\r\nconst val = obj && \"123\";\r\nconsole.log(val.slice()); // \u003c- runtime error!\r\n```\r\n\r\n**Expected behavior:**\r\nCompile error.\r\n\r\n**Actual behavior:**\r\nThis code compiles and causes a runtime error when run.\r\n\r\nTypeScript assumes that any value with object type (except `{}`) is truthy, but it's not *true* as illustrated above. If one of `0`, `\"\"`, `false` or `0n` is assignable to an object type, it should not be considered truthy.\r\n\r\n**Playground Link:** https://www.typescriptlang.org/play/#src=const%20obj%3A%20%7B%20toFixed%3A%20unknown%20%7D%20%3D%200%3B%0A%2F%2F%20val%20has%20type%20%22123%22%0Aconst%20val%20%3D%20obj%20%26%26%20%22123%22%3B%0Aconsole.log(val.slice())%3B%0A%0A%0A\r\n\r\n**Related Issues:** #18196 (fixed by #27157)\r\n","author":{"url":"https://github.com/uhyo","@type":"Person","name":"uhyo"},"datePublished":"2019-05-19T07:28:55Z","url":"https://github.com/microsoft/TypeScript/issues/31464"}
| 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:e8fb5d98-5538-4bcc-9adf-63649e68e349 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CDF4:10AF43:50B32E:6EA7FE:6B04A037 |
| html-safe-nonce | 57c23047b547d221eee2c9ebf84c65fcaf20191034e2c7204525f43e6eb47feb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDREY0OjEwQUY0Mzo1MEIzMkU6NkVBN0ZFOjZCMDRBMDM3IiwidmlzaXRvcl9pZCI6IjE5OTk0OTQyODA5NTk1MzMxMTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0cc38e2f44a566b7cce080419cc9840239061a0673ee6fd076f0d94a118300f3 |
| hovercard-subject-tag | issue:445789512 |
| 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/31464/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c7837c095b3993d9172ff111cc7b4693ce5a3f21484dba405f389924b986ecaa/microsoft/TypeScript/issues/31464 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c7837c095b3993d9172ff111cc7b4693ce5a3f21484dba405f389924b986ecaa/microsoft/TypeScript/issues/31464 |
| og:image:alt | TypeScript Version: 3.4.3, typescript@3.5.0-dev.20190518 Search Terms: unsound coercion object boolean Code const obj: { toFixed: unknown } = 0; // val has type "123" const val = obj && "123"; cons... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | uhyo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9784b6f42e2d5eac4e394fa88cad134c0dc5b6662b4ff2787a1ed1ef5a96331d |
| 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 | 3fd14143b75b2af7b76bbd23b069867d091adfcc |
| 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