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
The problem happens when Merge is used with two types that share one same key, and when the key is optional in the second type. The returned type for this property should be a union of the two initial types.
It's very strange, I don't expect that when I do an intersection. And TypeScript also seems to handles the case differently depending on whether you use the spread operator or an Object.assign. REPL
constresult2=Object.assign(Object.assign({},foo),bar);// infered as "Foo & Bar"typeFooBar=Foo&Bar;constresult3:FooBar={// <- ERROR Type '"b"' is not assignable to type '"a"'
...foo,
...bar}
The problem happens when
Merge
is used with two types that share one same key, and when the key is optional in the second type. The returned type for this property should be a union of the two initial types.Example
Given these two interfaces :
If we define two values typed as
Foo
andBar
:After merging these two objects, the inferred type is :
The optional property
baz
is typed as"b" | "a" | undefined
.Using
Merge
, the optional propertybaz
is typed as"b" | undefined
:"a"
is missing in the union here.I don't know if
Merge
should be changed to handle this use case or if a new type should be created ?The text was updated successfully, but these errors were encountered: