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
Generic lookup type wrongly narrowed to never with strictNullChecks #26130
This is just weird. Writing const x = element!.x;
eliminates the error, but there shouldn't be anything for ! to take away
FWIW !, as a cast, also resets a control flow-narrowed type to its declared type. We have an open issue to make it respect narrowing: #16945 (I was looking at it recently)
I found the problem: in getAssignmentReducedType, we have a declared type of {x: string} | null and an assigned type of {foo: {x: string} | null}[K] | null. When we ask if the assigned type is maybe assignable to {x: string}, we decompose the first level of union and check if {foo: {x: string} | null}[K] is assignable to {x: string}, but it isn't because its constraint is {x: string} | null. So we throw {x: string} out of the declared type and are left with null, and after the null check, we are left with never.
Using the comparable relation in place of typeMaybeAssignableTo fixes this issue. I'm going to see what else breaks if I do that.
Activity
RyanCavanaugh commentedon Aug 1, 2018
This is just weird. Writing
eliminates the error, but there shouldn't be anything for
!to take awayRyanCavanaugh commentedon Aug 1, 2018
Not a regression FWIW
weswigham commentedon Aug 2, 2018
FWIW
!, as a cast, also resets a control flow-narrowed type to its declared type. We have an open issue to make it respect narrowing: #16945 (I was looking at it recently)mattmccutchen commentedon Aug 2, 2018
I found the problem: in
getAssignmentReducedType, we have a declared type of{x: string} | nulland an assigned type of{foo: {x: string} | null}[K] | null. When we ask if the assigned type is maybe assignable to{x: string}, we decompose the first level of union and check if{foo: {x: string} | null}[K]is assignable to{x: string}, but it isn't because its constraint is{x: string} | null. So we throw{x: string}out of the declared type and are left withnull, and after the null check, we are left withnever.Using the comparable relation in place of
typeMaybeAssignableTofixes this issue. I'm going to see what else breaks if I do that.Have getAssignmentReducedType use the comparable relation instead of
mattmccutchen commentedon Aug 2, 2018
Proposed fix is in #26143.
Have getAssignmentReducedType use a new relation that checks for weak
Go back to the old narrowing algorithm (pre microsoft#26143) and avoid …
Merge pull request #26425 from mattmccutchen/issue-26405