-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object spread with Partial breaks in TS 2.4 #16509
Comments
That might actually be catching a bug, but I think it's running into typescript limitations. If Object.assign({a: 'a'}, {}) // => {a: 'a'} (matches {a: string})
Object.assign({a: 'a'}, {a: undefined}) // => {a: undefined} (does not match {a: string}) |
@Kovensky is right. Typescript does not currently distinguish between missing and undefined in most cases [1]. We discussed adding So 2.4 errs on the side of safety and prevents @Kovensky's second example from compiling at the cost of preventing the first, correct example from compiling. [1] The excess property check is an ad-hoc check carried out in a completely different way from other assignability checking. |
That's interesting. The language syntactically makes a distinction between
but you're saying they are typed the same? The best workaround I could find is to cast away from partial when you use it. Kinda disappointing though. Do you have any better suggestion? function update(f: Foo, newFields: Partial<Foo>) {
// no longer works:
// return {...f, ...newFields };
// workaround:
return {...f, ...(newFields as Foo) };
} |
This looks like an unintended consequence of #15938. untill we have a way to distinguish between |
@evmar basically yes. In this case we're going to make an exception for spread since I'll open an issue to track distinguishing between missing and undefined. |
Here's a stub for distinguishing missing and undefined: #16524 It still needs a lot more detail. |
Fix is up at #16525 |
You can no longer update an object using an object spread with Partial<T>. Seee bug: microsoft/TypeScript#16509 Work around it for now.
Thanks for the quick fix! I'll undo my workaround. |
You can no longer update an object using an object spread with Partial<T>. Seee bug: microsoft/TypeScript#16509 Work around it for now.
Test program works in TS2.3 and fails in TS2.4:
The text was updated successfully, but these errors were encountered: