You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tsc gets confused on checking assignability with numeric literals? #44087
TSC reports This condition will always return 'false' since the types 'A' and '-1' have no overlap.
For a moment I thought this might be another instance of #9998 or #8513, but from my understanding, the compiler ignores the -1 from the union type annotation of abc(A | -1) in , because number is not assignable to -1. So I thought this might be a different issue from #9998 or #8513, since the core issue seems to be that a literal type within a union expression gets ignored.
changed the title [-]This condition will always return 'false' since the types have no overlap[/-][+]tsc gets confused on checking assignability with numeric literals?[/+]on May 14, 2021
Ugh. We permit the assignment of number to A | -1 because, for ancient backwards compatibility reasons, number is assignable to any numeric enum type. Control flow analysis then narrows abc by removing all non-assignable types from its union type, thus believing abc has type A following the initialization. And that type doesn't include -1 so CFA now thinks the condition is always false.
We need to somehow exclude the number-assignable-to-any-enum rule from our control flow analysis. Ideally CFA sould view the initialization as having an unknown effect, thus not narrowing at all following the initialization.
Activity
[-]This condition will always return 'false' since the types have no overlap[/-][+]tsc gets confused on checking assignability with numeric literals?[/+]RyanCavanaugh commentedon May 14, 2021
This seems likely to be a side effect of having fixed #42442.
RyanCavanaugh commentedon May 14, 2021
cc Anders Hejlsberg (@ahejlsberg) and Daniel Rosenwasser (@DanielRosenwasser) for ideas on how to fix this 😕
ahejlsberg commentedon May 14, 2021
Ugh. We permit the assignment of
numbertoA | -1because, for ancient backwards compatibility reasons,numberis assignable to any numeric enum type. Control flow analysis then narrowsabcby removing all non-assignable types from its union type, thus believingabchas typeAfollowing the initialization. And that type doesn't include-1so CFA now thinks the condition is always false.We need to somehow exclude the number-assignable-to-any-enum rule from our control flow analysis. Ideally CFA sould view the initialization as having an unknown effect, thus not narrowing at all following the initialization.
RyanCavanaugh commentedon May 14, 2021
I would honestly propose Won't Fix and recommend writing
instead since the assignment is already unsound.
Piling up the epicycles higher will probably only make things worse.
RyanCavanaugh commentedon May 14, 2021
The disallowment of
abc === -1is a documented breaking change.Fixing the narrowing would be very invasive and noisy; we're going to leave this as-is.