question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'instanceof' changes type outside of 'if' statement

See original GitHub issue

TypeScript Version: 3.4.*

Search Terms:

instanceof

Code

// @strictNullChecks: true
interface OnChanges {
    onChanges(changes: Record<string, unknown>): void
}
interface Validator {
    validate(): null | Record<string, unknown>;
}

class C {
    validate() {
        return {}
    }
}
function foo() {
    let v: Validator & Partial<OnChanges> = null as any;
    if (v instanceof C) {
    }
    if (v.onChanges) { // error here
        v.onChanges({});
    }
}

Expected behavior:

No error.

Actual behavior:

Property 'onChanges' does not exist on type 'C | (Validator & Partial<OnChanges>)'.
  Property 'onChanges' does not exist on type 'C'.

instanceof shouldn’t change the type of the variable outside of the if statement.

This only happens with strictNullChecks enabled.

Playground Link: https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgPIgMIAs4gOYQDOyA3gFDKXID2mO+RAFAvQYQFzIBKEC1UAEwA8hMFFB4ANMgCuIANYhqAdxAA+AJScAbtWACyAXzKhIsRCgBqcADb64YfqQpVtt+5EZbkIGTZvIAD7cvPzCouL40nKKKuoA3EZkZAg2cITEGM5UyG52Ag4QXtk5VFAQYDJQIKTGOcbGMHIIYMC0yDDU1MXkOTYVuZzW+Q5OAGTIAApwUK22QujYuGxqyAC8Pn4B6ci4AJ6JOcAwyIzayKCiuEjUJxgaJZR1VMen2gB0tEsMhA+9pZQPl9WEwSIYNIcqA0gA

Related Issues:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
weswighamcommented, Apr 29, 2019

The correct fix is to make an intersection even if one of type or candidate is assignable to the other - unions only do subtype reduction, so anything that narrows to a type by assignability (rather than subtype) will have this issue.

0reactions
ortacommented, Feb 16, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java - alternative to many else if statements with instanceof ...
Firstly, I would have them implementing a common interface or superclass. This is so you don't need to test what type of class...
Read more >
3 Pattern Matching for the instanceof Operator
Pattern matching involves testing whether an object has a particular structure, then extracting data from that object if there's a match.
Read more >
Java “instanceOf”: Why And How To Avoid It In Code - Armedia
It is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we...
Read more >
instanceof - JavaScript - MDN Web Docs
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.
Read more >
Semantics - The Apache Groovy programming language
If left out, the type name will be deemed to refer to an existing variable (presumably ... Despite not changing the semantics of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found