Type guards on variable break refinements on its properties #18840
Labels
Fixed
A PR has been merged for this issue
In Discussion
Not yet reached consensus
Suggestion
An idea for TypeScript
TypeScript Version: 2.5.2 & nightly (2.6.0-dev.20170929)
Code
Expected behavior:
Given that the first line of the body of
f
checks whetherx.foo
is a string, the last line of the body should not cause an error.Actual behavior:
The compiler shows an error on the last line of the body of
f
, since the type ofx.foo
is (wrongly) resolved tostring | number
instead ofstring
. The type guard on the first line off
is not considered.I think that this happens because the compiler only searches for property type guards (on
x.foo
in this case) until the last refinement on the related variable (in this casex
). It would be better if it would search until the last assignment, as type guards onx
should not break any refinement on properties.Note that the correct behavior can be found when commenting the line that declares
y
.The text was updated successfully, but these errors were encountered: