-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Comments
What do you expect the compiled output to be in this case? If I'm not mistaken the right side of |
But |
@robertpenner Is there a way to make T only be of class type? If not then requesting this as new feature or issue:
|
The best you could probably do is change your function to take an extra parameter which is a constructor function for type 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 |
@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 |
@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 |
The type system doesn't provide a way to "work backwards" from a type to some constructor function for that type. Taking in a |
Automatically insert generic when using instanceof:
this now, gives an error "cannot find name'T'"
The text was updated successfully, but these errors were encountered: