-
Notifications
You must be signed in to change notification settings - Fork 12.8k
typechecking of return types broken with try/catch
when strictNullChecks
and noImplicitAny
are enabled
#45592
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
When we return, we look at all possible paths of control flow and form a union. We see we have |
How come turning off |
I understand that |
When TypeScript originally introduced the Here, the
Yeah, I agree, this is not desirable. Personally, I try to steer clear of auto-typed variables. I often need to include a type anyway since they get initialized in complex ways with The only thing we could probably do now is to provide a flag for forbidding assignments of |
What surprises me most is that this doesn't get flagged as a "not definitely assigned in all branches" error. It seems like that should happen regardless, even in the presence of |
So we do if you provide an explicit type for your variable. Check it out here. We don't in the original post, because the analysis to decide if something is uninitialized checks whether the original type was declared without But here, there's no explicit type, so you get no |
One thing we could possibly do is create a special variant of |
Ah, I see, I was thinking of this error message (see #11263):
but now that I've done some more digging, it looks like that only applies in cases related to #9998 (i.e. closing over the auto-typed variable). That said, what is the mechanism used to produce that error, if it's different from this case? |
Bug Report
π Search Terms
try/catch, exception handling, strict mode
π Version & Regression Information
Broken in 4.3.5, still broken in nightly
β― Playground Link
Playground link with strict mode
Disable
--noImplicitAny
and--strict
(but leave--strictNullChecks
) and typechecker will correctly note thatval
may have type nullπ» Code
π Actual behavior
Compile with
--strict
and observe no errorsCompile simply with
--strictNullChecks
and typechecker will correctly note thatval
may have type null and does not conform to the return typeπ Expected behavior
typechecker should correctly determine that
val
can have type null and does not conform to the return typeThe text was updated successfully, but these errors were encountered: