Skip to content

Incorrect null check compilation error #27282

Closed

Description

@joyzhong

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms:
null check, null check compilation

Code

declare const x: (() => void)|null;
declare const ctor: Function;

if (x instanceof ctor) {
  x();
}

Expected behavior:
No compilation error since if x == null => true, then x instanceof Function => false

Actual behavior:
Cannot invoke an object which is possibly 'null'.
const x: (() => void) | null

Playground Link: http://www.typescriptlang.org/play/#src=declare%20const%20x%3A%20%7By%3A%20()%20%3D%3E%20%27hi%27%7Cnull%7D%3B%0D%0Adeclare%20const%20ctor%3A%20Function%3B%0D%0A%0D%0Aif%20(x.y%20instanceof%20ctor)%20%7B%0D%0A%20%20x.y()%3B%0D%0A%7D

Related Issues:

Activity

  1. joyzhong commented on Sep 21, 2018

    @joyzhong
    Author

    (Note that strictNullChecks must be enabled in Options)

  2. rolivegab commented on Sep 22, 2018

    @rolivegab

    The following code has no compile errors:

    declare const x: (() => void) | null
    declare const y: FunctionConstructor
    
    if (x instanceof y) {
    	x()
    }
    

    VSCode editor also points:
    image

    But i don't know really what any of that means

  3. ghost added
    BugA bug in TypeScript
    on Sep 22, 2018
  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions