platform icon indicating copy to clipboard operation
platform copied to clipboard

feat(entity): strengthen typing of getInitialState

Open timdeschryver opened this issue 5 months ago • 2 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/ngrx/platform/blob/main/CONTRIBUTING.md#commit
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [ ] Documentation has been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Closes #4422

When initializing an EntityAdapter<T> and subsequently setting the initialState using ...adapter.getInitialState({ }), it is possible, given the current interface, to introduce unknown variables that do not conform to the user's specified state. This could result in bugs related to mistyped variable names, resulting in confusing user error scenarios.

What is the new behavior?

This small change modifies the interface to require that the input state S extends EntityState<T>, requires that the additionalState state argument conforms to an Omit version of this S type, which removes the 'id' and 'entities' properties by way of keyof EntityState<T>, and then finally, since the argument now already extends EntityState<T>, we can now simply return S.

Does this PR introduce a breaking change?

[x] Yes
[ ] No

Other information

This reopens #4423 (with an update of the branch) - with the upcoming release this is a good timing imho.

timdeschryver avatar Jun 05 '25 11:06 timdeschryver

Deploy Preview for ngrx-io canceled.

Name Link
Latest commit c1892a4feb10afb45e30f31397df8f8d9349b2b9
Latest deploy log https://app.netlify.com/projects/ngrx-io/deploys/686253967c59840008e5536c

netlify[bot] avatar Jun 05 '25 11:06 netlify[bot]

Deploy Preview for ngrx-site-v19 ready!

Name Link
Latest commit c1892a4feb10afb45e30f31397df8f8d9349b2b9
Latest deploy log https://app.netlify.com/projects/ngrx-site-v19/deploys/6862539681fcc80008c074fd
Deploy Preview https://deploy-preview-4819--ngrx-site-v19.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Jun 05 '25 11:06 netlify[bot]

Thanks very much for closing this out @timdeschryver!

GavynHolt avatar Jun 30 '25 11:06 GavynHolt

Thank you for bringing this up and opening a PR @GavynHolt!

timdeschryver avatar Jun 30 '25 18:06 timdeschryver