Skip to content
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

generic instanceof #5236

Closed
ken-blocklevel opened this issue Oct 13, 2015 · 11 comments
Closed

generic instanceof #5236

ken-blocklevel opened this issue Oct 13, 2015 · 11 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@ken-blocklevel
Copy link

Automatically insert generic when using instanceof:

    private static success <T> (check: T | boolean) : boolean
    {
        return typeof check === 'boolean' ? check : check instanceof T;
    }

this now, gives an error "cannot find name'T'"

@jesseschalken
Copy link
Contributor

What do you expect the compiled output to be in this case? If I'm not mistaken the right side of instanceof is an expression evaluating to a constructor function (ie. a class), not a type (instanceof being a real thing that exists in JavaScript). It's not going to work with an arbitrary type.

@robertpenner
Copy link

But T can be an interface, which doesn't exist at runtime.

@ken-blocklevel
Copy link
Author

@robertpenner Is there a way to make T only be of class type? If not then requesting this as new feature or issue:

private static success <T is class> (check: T | boolean) : boolean
{
    return typeof check === 'boolean' ? check : check instanceof T;
}

@jesseschalken
Copy link
Contributor

The best you could probably do is change your function to take an extra parameter which is a constructor function for type T and use that to do the instanceof:

class Foo {
    public blah() {
    }
}

function success<T>(check:T|boolean, constructor:{new ():T}):boolean {
    return typeof check === 'boolean' ? check : check instanceof constructor;
}

success<Foo>(new Foo, Foo);

@jesseschalken
Copy link
Contributor

@ken-blocklevel You can do T extends Object, but you still can't do an instanceof without a constructor function for T.

@ken-blocklevel
Copy link
Author

@jesseschalken Good solution, but one must admit not a very clean one. Memorywise making a new object isn't efficient, besides you don't know what parameter T's constructor takes seeing it is variable...

@jesseschalken
Copy link
Contributor

@ken-blocklevel Yeah, you can probably do new (...args: any[]):T so any set of parameters will fit, or something.

@ken-blocklevel
Copy link
Author

@jesseschalken Good one again, thank you. Do you believe this proposal is unnecessary or redundant?

@jesseschalken
Copy link
Contributor

@ken-blocklevel If passing the constructor function around works, then I think so. TypeScript could probably do what you originally wanted with a lot of assumptions and complexity (like assuming T is a class, then passing the constructor function through via a hidden parameter).

@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Oct 13, 2015
@RyanCavanaugh
Copy link
Member

The type system doesn't provide a way to "work backwards" from a type to some constructor function for that type. Taking in a new(... args: any[]): T is the intended solution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

4 participants