Given a control flow where one of the branches throws an error, I should be able to use an error throwing function and have it typecheck correctly, but that is not the case right now. For example:
function fail() {
throw new Error();
}
function foo(a: string): string {
switch(a) {
case 'bar':
return 'bar';
default:
throw new Error();
}
}
function foo2(a: string): string {
switch(a) {
case 'bar':
return 'bar';
default:
// $ExpectError
fail();
}
}
Flow version: 0.104.0
It should typecheck.
It fails typecheck with the error: "Cannot expect string as the return type of function because string [1] is incompatible with implicitly-returned undefined."
Try adding return before fail().
function fail() {
throw new Error();
}
function foo(a: string): string {
switch(a) {
case 'bar':
return 'bar';
default:
throw new Error();
}
}
function foo2(a: string): string {
switch(a) {
case 'bar':
return 'bar';
default:
return fail();
}
}
If you want to be more secure, you might want to use unions of strings and make sure you cover them all already during compilation time without actually needing to throw during runtime.
Something like:
function fail(val: empty) {
throw new Error();
}
function foo2(a: 'foo' |聽'bar'): string {
switch(a) {
case 'bar':
return 'bar';
case 'foo':
return 'bar';
default:
return fail(a);
}
}
Here is an interesting article about exhaustiviness & Flow: https://blog.jez.io/flow-exhaustiveness/
Thanks, that works!
@villesau this looks more like bug that this PR https://github.com/facebook/flow/pull/7658 or this https://github.com/facebook/flow/pull/7657 were trying to fix