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
an optional arg of type Bar[K] | null can not receive null #13826
changed the title [-]a optional arg of type `Bar[K] | null` can not receive `null`[/-][+]an optional arg of type `Bar[K] | null` can not receive `null`[/+]on Feb 2, 2017
This cause of this rather involved: When we add undefined to a parameter declared with ?, we perform subtype reduction on the resulting union type. So, we construct the type Bar[K] | null | undefined and then subtype reduce that type. That eventually causes us to check whether null is a subtype of Bar[K], which we say is true if null is a subtype of the apparent type of Bar[K]. Since the apparent type of Bar[K] is Bar['bar' | 'foo'], which reduces to number | object | null, we end up concluding it is a subtype and therefore remove null from the parameter type.
I think the core issue here is that we use the same apparent type algorithm for a lookup type for both reading and writing. We union the types of the properties in the underlying type, which is correct for reading, but we really should use an intersection when checking for writing.
Anyway, a simple fix is to not do subtype reduction in the the call to getUnionType in the includeFalsyTypes function. There's really no meaningful rationale for the subtype reduction there and the fix has no effect on our baselines.
Reacted by Ryan Cavanaugh, Herrington Darkholme and Aluan Haddad
Activity
[-]a optional arg of type `Bar[K] | null` can not receive `null`[/-][+]an optional arg of type `Bar[K] | null` can not receive `null`[/+]gdh1995 commentedon Feb 2, 2017
I find a work-around:
b?: Bar[K] | null | undefinedis OKay. So I wonder if this isworking as indended.sandersn commentedon Feb 2, 2017
No, definitely a bug. This is very strange!
sandersn commentedon Feb 2, 2017
Here's another repro showing that it's probably instantiation of the index type:
Note that deleting all properties that include the type
null(foo, in this example) also makes the error go away.ahejlsberg commentedon Feb 2, 2017
This cause of this rather involved: When we add
undefinedto a parameter declared with?, we perform subtype reduction on the resulting union type. So, we construct the typeBar[K] | null | undefinedand then subtype reduce that type. That eventually causes us to check whethernullis a subtype ofBar[K], which we say is true ifnullis a subtype of the apparent type ofBar[K]. Since the apparent type ofBar[K]isBar['bar' | 'foo'], which reduces tonumber | object | null, we end up concluding it is a subtype and therefore removenullfrom the parameter type.I think the core issue here is that we use the same apparent type algorithm for a lookup type for both reading and writing. We union the types of the properties in the underlying type, which is correct for reading, but we really should use an intersection when checking for writing.
Anyway, a simple fix is to not do subtype reduction in the the call to
getUnionTypein theincludeFalsyTypesfunction. There's really no meaningful rationale for the subtype reduction there and the fix has no effect on our baselines.sandersn commentedon Feb 2, 2017
Anders Hejlsberg (@ahejlsberg) do you want me to write up the fix or do you want to do it?
No subtype reduction in includeFalsyTypes
sandersn commentedon Feb 7, 2017
Fix is up at #13930
vilicvane commentedon Feb 27, 2017
Mohamed Hegazy (@mhegazy) Nathan Shively-Sanders (@sandersn) A similar bug still exists for function parameter:
krisselden commentedon Mar 2, 2017
possible cause of #14406