Title: 'instanceof' changes type outside of 'if' statement · Issue #31155 · microsoft/TypeScript · GitHub
Open Graph Title: 'instanceof' changes type outside of 'if' statement · Issue #31155 · microsoft/TypeScript
X Title: 'instanceof' changes type outside of 'if' statement · Issue #31155 · microsoft/TypeScript
Description: TypeScript Version: 3.4.* Search Terms: instanceof Code // @strictNullChecks: true interface OnChanges { onChanges(changes: Record
Open Graph Description: TypeScript Version: 3.4.* Search Terms: instanceof Code // @strictNullChecks: true interface OnChanges { onChanges(changes: Record
X Description: TypeScript Version: 3.4.* Search Terms: instanceof Code // @strictNullChecks: true interface OnChanges { onChanges(changes: Record<string, unknown>): void } interface Validator { validate(): ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31155
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"'instanceof' changes type outside of 'if' statement","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.4.*\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\n\r\ninstanceof\r\n\r\n**Code**\r\n\r\n```ts\r\n// @strictNullChecks: true\r\ninterface OnChanges {\r\n onChanges(changes: Record\u003cstring, unknown>): void\r\n}\r\ninterface Validator {\r\n validate(): null | Record\u003cstring, unknown>;\r\n}\r\n\r\nclass C {\r\n validate() {\r\n return {}\r\n }\r\n}\r\nfunction foo() {\r\n let v: Validator & Partial\u003cOnChanges> = null as any;\r\n if (v instanceof C) {\r\n }\r\n if (v.onChanges) { // error here\r\n v.onChanges({});\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo error.\r\n\r\n**Actual behavior:**\r\n\r\n```\r\nProperty 'onChanges' does not exist on type 'C | (Validator & Partial\u003cOnChanges>)'.\r\n Property 'onChanges' does not exist on type 'C'.\r\n````\r\n\r\n`instanceof` shouldn't change the type of the variable outside of the `if` statement.\r\n\r\nThis only happens with `strictNullChecks` enabled.\r\n\r\n**Playground Link:** https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgPIgMIAs4gOYQDOyA3gFDKXID2mO+RAFAvQYQFzIBKEC1UAEwA8hMFFB4ANMgCuIANYhqAdxAA+AJScAbtWACyAXzKhIsRCgBqcADb64YfqQpVtt+5EZbkIGTZvIAD7cvPzCouL40nKKKuoA3EZkZAg2cITEGM5UyG52Ag4QXtk5VFAQYDJQIKTGOcbGMHIIYMC0yDDU1MXkOTYVuZzW+Q5OAGTIAApwUK22QujYuGxqyAC8Pn4B6ci4AJ6JOcAwyIzayKCiuEjUJxgaJZR1VMen2gB0tEsMhA+9pZQPl9WEwSIYNIcqA0gA\r\n\r\n**Related Issues:**\r\n","author":{"url":"https://github.com/ajafff","@type":"Person","name":"ajafff"},"datePublished":"2019-04-29T07:29:16Z","url":"https://github.com/microsoft/TypeScript/issues/31155"}
| 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:f114b0da-fccb-393f-cd5d-ca6cfd78fa80 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D24C:65478:31AD581:41EC6D2:6B01E326 |
| html-safe-nonce | 95f88ca456a863868b430d636ca50e2bf98e7e5b182db6acfd6f1752c9c0eda0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjRDOjY1NDc4OjMxQUQ1ODE6NDFFQzZEMjo2QjAxRTMyNiIsInZpc2l0b3JfaWQiOiIxMjYxODk3MjA5MDkyMzY3MTQyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7e259e30934a5fc5c446f2e91e47ac14568d2cabfab0394451297fc60e712e09 |
| hovercard-subject-tag | issue:438182509 |
| 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/31155/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff951d0d8809cd99d12130a5487d82044510ecce33f9e198d7d1c9128908ae4f/microsoft/TypeScript/issues/31155 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff951d0d8809cd99d12130a5487d82044510ecce33f9e198d7d1c9128908ae4f/microsoft/TypeScript/issues/31155 |
| og:image:alt | TypeScript Version: 3.4.* Search Terms: instanceof Code // @strictNullChecks: true interface OnChanges { onChanges(changes: Record |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ajafff |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width