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
Possibility of undefined from optional properties is not honoured in spread #35983
Here's an example of spreading an object with optional properties:
constmyQueryDefaults: {a: number;}={a: 1};constmyQuery: {a?: number;}={};constresult={ ...myQueryDefaults, ...myQuery};// type is `number`// runtime value is `number`result.a;
The above example makes sense. However, an optional property does not only mean "this property may not exist" ({})—it also means "this property may exist with a value of undefined" ({ a: undefined }). (Related: #13195.)
As a result of this, the type information may mismatch the actual types at runtime:
constmyQueryDefaults: {a: number;}={a: 1};constmyQuery: {a?: number;}={a: undefined};constresult={ ...myQueryDefaults, ...myQuery};// type is `number`, but runtime value is `undefined`!!// expected type: `number | undefined`result.a;
… which could result in runtime exceptions:
result.a.toString();// runtime exception, because `result.a` is `undefined`
Ideally optional properties would only mean "this property may not exist" (#13195), in which case the existing behaviour with spreading makes sense. However, in the interim, perhaps the behaviour of spreading should be changed?
changed the title [-]`undefined` Optional properties are not honoured in spread[/-][+]Possibility of `undefined` from optional properties is not honoured in spread[/+]on Jan 3, 2020
Activity
[-]`undefined` Optional properties are not honoured in spread[/-][+]Possibility of `undefined` from optional properties is not honoured in spread[/+]MartinJohns commentedon Jan 3, 2020
That would most likely break every codebase out there.
RyanCavanaugh commentedon Jan 6, 2020
#13195 is the actionable version of this. This would be an unbelievably large and unergonomic breaking change.