-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS 3.2.0-rc seems to forget union type refinements more eagerly #28568
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
Maybe it guess x.body.toString() may has side effect and fail the refinements? |
Oh, no, removing that line has no effect. I just added it to demonstrate that the initial refinement is working. |
I can't reproduce a difference between 3.1 and 3.2. In As an aside, the error in |
That is odd. This is an error I only encountered in existing code once I updated to TS 3.2, but my snippet indeed also displays the error in 3.1... |
Oh, I found the version that errors in 3.2 but not 3.1; both with function test(x: { type: 'a'; body: string | null } | { type: 'b'; body: number | null }): string {
if (x.body === null) {
return 'null'
}
switch (x.type) {
case 'a':
return x.body.toString() // x.body may be null
case 'b':
return x.body.toString() // x.body may be null
}
} |
@Kovensky Yes, that is due to #27612. We weren't correctly resetting our narrowing assumptions in |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.2.0-rc
Search Terms:
refinement
Code
(EDIT: incorrect reproduction, see correct example at #28568 (comment))
Expected behavior:
The refinement of
x.body
should persist (it does in 3.1)Actual behavior:
TS forgets that it has already refined
x.body
to be notnull
.The text was updated successfully, but these errors were encountered: