Skip to content

Questioning the way how you deep merge a partial JSON into the original JSON? #82

@binarykitchen

Description

@binarykitchen

Buona sera,

I'm experiencing a TypeScript conflict while deep merging a partial JSON into the original JSON using your latest version 3.2.0.

Error Message

Type 'PartialObjectDeep<VideomailRecorderOptions, { recurseIntoArrays: false; allowUndefinedInNonTupleArrays: false; }>' is not assignable to type 'VideomailRecorderOptions' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Property 'apiUrl' is optional in type 'PartialObjectDeep<VideomailRecorderOptions, { recurseIntoArrays: false; allowUndefinedInNonTupleArrays: false; }>' but required in type 'VideomailRecorderOptions'.

Why should I add undefined to the property apiURL to VideomailRecorderOptions when it must remain defined during a deep merge from a partial one? The target already has it defined and would crash my app. No, thanks.

Code

That code is:

const newOptions: VideomailRecorderOptions = deepmerge({
  onlyDefinedProperties: true,
})(state, action.payload);

Link:
https://github.com/binarykitchen/videomail-recorder/blob/main/src/features/options/reducers.ts#L15

As you can see, it's Redux, merging the payload back to the original state the immutable way. Only with defined properties.

Steps to reproduce

  1. Git clone git@github.com:binarykitchen/videomail-recorder.git
  2. npm install
  3. npm run types
  4. Errors in the console ...

Summary

All I am after is just to deep merge partial options in JSON format into the original state in JSON, returning a new JSON.

The partial option typings stem from the original option typings this way:

type PartialVideomailRecorderOptions = PartialDeep<VideomailRecorderOptions>;

This PartialDeep helper function comes from the type-fest library, which is well-tested:
https://github.com/sindresorhus/type-fest

Appreciate any feedback.

Grazie 🙇

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions