-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Object union member functions that return never are incorrectly narrowed #56425
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
Comments
I think that was the point - the program will terminate if |
Possibly related to #49771, but not sure that fully covers the use case here |
A function which claims to return
That would be typed/checked exceptions which is #56365 (Ryan needs to update the description there, methinks). |
Does flow need to track the try blocks or not? E.g., a return type of |
Narrowing in try blocks is applied only if the catch block terminates, this is how it works now: // returns number | undefined
function destring(value: string | number) {
try {
if (typeof value == 'string') {
throw new Error("string_error");
}
// value: number
} catch {
// value: string | number
return undefined;
}
// value: number
return value
} It would work the same way here, whether an exception was thrown or not wouldn't matter. // returns Bar | undefined
function try_defoo(baz: Baz) {
try {
baz.defoo();
// baz: Bar
} catch (e) {
// baz: Baz
return undefined;
}
// baz: Bar
return baz;
} |
Reflecting the never/throws return type back onto the flow of the base object is definitely a good idea. |
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
"never", "class union", "narrowing unions", "never union"
π Version & Regression Information
β― Playground Link
Playground Link
π» Code
π Actual behavior
baz
doesn't get narrowed after calling.defoo()
, even though it's impossible for any value after that point to beFoo
. This seems to happen because the type ofBaz["defoo"]
is equal toFoo["defoo"] | Bar["defoo"]
or() => never | void
.π Expected behavior
baz
should narrow toBar
after calling.defoo()
because the return type ofFoo.defoo
isnever
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: