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
Union type narrowing with generics not working in TS 3.5 RC strict mode #31456
This is an effect of #30637. Previously, unconstrained type parameters were treated as if they had the constraint {} and therefore could never be null or undefined. That isn't correct because an unconstrained T can be instantiated at any type, including null or undefined. In 3.5 we now check it correctly. That specifically means that bar !== undefined doesn't imply that T disappears from the type in the false branch.
If you explicitly add a constraint to T you get the expected behavior:
Activity
ahejlsberg commentedon May 18, 2019
This is an effect of #30637. Previously, unconstrained type parameters were treated as if they had the constraint
{}and therefore could never benullorundefined. That isn't correct because an unconstrainedTcan be instantiated at any type, includingnullorundefined. In 3.5 we now check it correctly. That specifically means thatbar !== undefineddoesn't imply thatTdisappears from the type in the false branch.If you explicitly add a constraint to
Tyou get the expected behavior: