-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
TypeScript Version: 1.8.0 and 2.0.0 beta
Code
const obj = Object.assign({}, { a: 'IamAString' }, { a: 33 })Expected behavior:
We need a stricter behavior when we want to transform an existing object in a way that respect its type. Object.assign has a broader scope that this alone, but if one were to write his own merge function that used an intersection of types as the result, the same would happen.
Given a source object of type T and an update of type U, perhaps what's missing is being able to express the constraint "U is a sub/partial type of T"; Writing T extends U is too strong a constraint; for instance if the source object T has an optional property, we can never update it as { prop?: string } is not a valid sub type of { prop: string }
Actual behavior:
The compiler won't complain even though the second object's a key has an incompatible type with the first object's a key. T & U also intersects nested properties resulting in a lot of subsequent silent errors (when reading properties of a now too broad type) and strange type combinations (string & number ?_?)