Skip to content

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

Description

@Methuselah96

馃悰 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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions