Skip to content

instanceof doesn't work if Function interface changes #1228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rjamesnw opened this issue Nov 20, 2014 · 3 comments
Closed

instanceof doesn't work if Function interface changes #1228

rjamesnw opened this issue Nov 20, 2014 · 3 comments
Assignees
Labels
Bug A bug in TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@rjamesnw
Copy link

Extending the Function interface should not cause "instanceof" to fail. Adding new items shouldn't change the fact that it's still a Function instance type. (see http://goo.gl/mCb93P)

interface Function {
    [index: string]: any;
}

class Base { }
class Greeter extends Base {
    greeting: string;
    constructor(message: string) {
        super();
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

var greeter = new Greeter("world");

var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
    alert(greeter.greet());
}

document.body.appendChild(button);

var b: Base;
(b instanceof Greeter); <== 'Greeter' is an ERROR

The whole error message is "The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type."

The expected behaviour is that anything that is of type Function would inherit this same change, and thus should not cause errors.

@danquirk danquirk added the Bug A bug in TypeScript label Nov 20, 2014
@billti billti added this to the TypeScript 1.4 milestone Nov 21, 2014
@vladima
Copy link
Contributor

vladima commented Nov 22, 2014

Technically current behavior is by design, we add only properties defined in Function type

3.10.1 Apparent Members
The apparent members of an object type T are the combination of the following:

  • The declared and/or inherited members of T.
  • The properties of the global interface type 'Object' that aren't hidden by properties with the same name in T.
  • If T has one or more call or construct signatures, the properties of the global interface type 'Function' that aren't hidden by properties with the same name in T.

Out of curiosity, what is your use case that you need to add indexer to Function type.

@rjamesnw
Copy link
Author

The code was originally from a project prototype that was to implement a type of web OS, and certain hidden properties were placed on the function object's static object (in regards to how meta data was being stored within the name spaces [this included Object, Function, Array, Data, RegExp, etc.]). It's a very special case at the time (things are done differently now), but it's something I did notice when copying over some old code. Just thought I'd mention it to see why, as regardless of use case, I figured it should be expected to work anyway. I think a use case is fine to prioritize things, but not to decide what should be expected to work (IMHO). To be honest, I don't really care - just an FYI. ;)

@danquirk
Copy link
Member

The design rationale here is roughly the same as for Object in #835. May warrant a second look given the recent set of people having issues here (ex #1232)

@mhegazy mhegazy added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead and removed By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Dec 11, 2014
@mhegazy mhegazy closed this as completed Dec 11, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

5 participants