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
Refinement with instanceof and generic unions is unsound
#6741
Comments
|
Needless to say, this yields // @flow
function coerce<T, U>(t: T): U {
class B<X> {
x: X;
constructor(x) {
this.x = x;
}
}
function b<X>(x: X | B<X>): B<X> {
if (x instanceof B) return x;
else throw new Error("Unreachable.");
}
const bb: B<B<{t: T}>> = b(new B({t}));
if (!(bb.x instanceof B)) {
return bb.x.t;
}
throw new Error("Unreachable.");
}
const twelve: number = coerce("twelve"); |
instanceof and generic unions is unsoundinstanceof and generic unions is unsound
|
cc @samwgoldman |
|
After speaking to @samwgoldman , we think it's possible that we are refining x to Thank you for reporting, @wchargin! |
|
Yeah, see Line 3995 in 7057f04
Worth experimenting with using |
Makes sense; this sounds like the right semantics. It would be nice for |
If
x: T | MyClass<T>, then Flow infers fromx instanceof MyClassthat
x: MyClass<T>. This is unsound:Tcould beMyClass<U>forsome other
U, so that in factx: MyClass<MyClass<U>>.Here is an example of how this can go wrong:
The text was updated successfully, but these errors were encountered: