Skip to content

Extending global Function interface breaks instanceof Type Guard #26159

Closed

Description

@kobiburnley

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms:
global Function interface
instanceof
Type Guard

Code

declare global {
  interface Function {
    now(): string;
  }
}

Function.prototype.now = function () {
  return "now"
}

class X {
  static now() {
    return {}
  }

  why() {

  }
}

class Y {

}

console.log(X.now()) // works as expected
console.log(Y.now()) // works as expected

export const x: X | number = Math.random() > 0.5 ? new X() : 1

if (x instanceof X) {
  x.why() // should compile
}

Expected behavior:
Code compiles

Actual behavior:
Compilation failed
"Property 'why' does not exist on type 'number'."

Playground Link: Playground

Related Issues:
#7271

Activity

  1. ghost added
    BugA bug in TypeScript
    on Aug 2, 2018
  2. kobiburnley commented on Aug 2, 2018

    @kobiburnley
    Author

    Andy (Andrewkraft) (@Andy-MS) You're right, I forgot to emphasize on this. It happens for classes that their static fields override some new added properties to the Function interface.

    But this works as expected though:

    console.log(X.now()) // works as expected
    console.log(Y.now()) // works as expected
  3. 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 TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions