Skip to content
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

Draft type no longer handles nested readonly objects with mixed property value types correctly #1013

Closed
3 tasks done
Methuselah96 opened this issue Jan 16, 2023 · 2 comments · Fixed by #1014
Closed
3 tasks done
Labels

Comments

@Methuselah96
Copy link
Contributor

Methuselah96 commented Jan 16, 2023

🐛 Bug Report

The fix in #990 does not seem to properly fix #839 and also breaks other scenarios. It should probably be reverted.

That PR "fixes" the issue of recursive types by bailing out of the recursion if the property's value type does not extend object. This "works" in the case of JsonObject because JsonObject's property's value type is a union of objects and primitives, and so it doesn't recurse on the object's properties because the property's value type does not extend the object type.

This is problematic because it won't remove the readonly modifier from nested objects if they have mixed property value types (both in the case of the JsonObject type and in other cases).

Link to repro

import { produce } from 'immer';

interface TestReadonlyObject {
    // The property value type does not extend object, so it will not recursively make the object writable.
    readonly testObjectOrNull: { readonly testProperty: number } | null;
}

const input: TestReadonlyObject = { testObjectOrNull: null };

produce(input, (draft) => {
    if (draft.testObjectOrNull) {
        // Errors because `testProperty` is still readonly, even though it shouldn't be.
        draft.testObjectOrNull.testProperty = 5;
    }
});

Playground Link

To Reproduce

See above.

Observed behavior

There is a TypeScript error.

Expected behavior

There should be no TypeScript error.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: 9.0.18
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@Methuselah96 Methuselah96 changed the title Draft type no longer handles deeply nested readonly object with mixed property type correctly Draft type no longer handles deeply nested readonly object with mixed property value types correctly Jan 16, 2023
@Methuselah96 Methuselah96 changed the title Draft type no longer handles deeply nested readonly object with mixed property value types correctly Draft type no longer handles nested readonly objects with mixed property value types correctly Jan 16, 2023
@SLKnutson
Copy link

Hi,
Can you release this change? The break went out patch release that has a tendency to automatically get pulled in.
Thanks!

@github-actions
Copy link
Contributor

🎉 This issue has been resolved in version 9.0.19 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants