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
typemytype={p1: string;}functionconstructMyType_error(): mytype{leto: Partial<mytype>={};o.p1='x';// Error "Type 'undefined' is not assignable to type 'string'"returno;}functionconstructMyType_noerror(): mytype{leto: Partial<mytype>={};o.p1='x';// No error.return{p1: o.p1};}
Expected behavior:
No compiler errors.
Actual behavior:
A compiler error in the function constructMyType_error, although the compiler knows that the field p1 has type string instead of string | undefined. It does not use this knowledge to conclude that the variable o is compatible with the return type.
Why is this problematic
In some cases it is nice to construct an object one property at a time. Without loosing any compiler checks, that is. And without having to write the code to copy the object, as the function constructMyType_noerror does.
The text was updated successfully, but these errors were encountered:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.5.2
Code
Expected behavior:
No compiler errors.
Actual behavior:
A compiler error in the function constructMyType_error, although the compiler knows that the field p1 has type
string
instead ofstring | undefined
. It does not use this knowledge to conclude that the variable o is compatible with the return type.Why is this problematic
In some cases it is nice to construct an object one property at a time. Without loosing any compiler checks, that is. And without having to write the code to copy the object, as the function constructMyType_noerror does.
The text was updated successfully, but these errors were encountered: