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
StrictNullChck with Object.assign anomaly (Object is possibly 'undefined') #43110
The value {fields: null} is not assignable to type {fields?: string}. Optional properties can be undefined, not null. Is this a mistake?
Object.assign() produces an intersection type, which is only an approximation of what happens when there is property overlap, see Bug Report + Proposal: Object.assign() #31982 for why.
Since Reduce intersections by discriminants #36696, the intersection that comes out of Object.assign() here, {fields: null} & ({ fields?: string , strict?: boolean} | undefined), will be reduced to never. Apparently the reduction to never here makes the compiler forget about any control flow narrowing for options, and it resets to the original type of options, which includes undefined in its domain. When you make any of the changes you mention above, this reduction to never doesn't happen, because there's no longer an impossible discriminant type.
This is a combination of things working as intended and known design limitations. If you think there's something here that isn't explained by the above points, could you point it out?
Reacted by Daniel Rosenwasser, Martin Johns and Geza Radics
Activity
jcalz commentedon Mar 6, 2021
There's a number of issues mixed together here:
{fields: null}is not assignable to type{fields?: string}. Optional properties can beundefined, notnull. Is this a mistake?Object.assign()produces an intersection type, which is only an approximation of what happens when there is property overlap, see Bug Report + Proposal: Object.assign() #31982 for why.Object.assign()here,{fields: null} & ({ fields?: string , strict?: boolean} | undefined), will be reduced tonever. Apparently the reduction toneverhere makes the compiler forget about any control flow narrowing foroptions, and it resets to the original type ofoptions, which includesundefinedin its domain. When you make any of the changes you mention above, this reduction toneverdoesn't happen, because there's no longer an impossible discriminant type.This is a combination of things working as intended and known design limitations. If you think there's something here that isn't explained by the above points, could you point it out?
holdfenytolvaj commentedon Mar 6, 2021
Thanks for the detailed answer