Typescript: generic instanceof

Created on 13 Oct 2015  路  11Comments  路  Source: microsoft/TypeScript

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'"

By Design

Most helpful comment

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);

All 11 comments

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.

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

@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;
}

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);

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

@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...

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

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

@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).

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielRosenwasser picture DanielRosenwasser  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

uber5001 picture uber5001  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments