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

Feat: add some overloads for StoreSetter #1556

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SarguelUnda
Copy link
Contributor

@SarguelUnda SarguelUnda commented Feb 14, 2023

Summary

At the moment typescript only allow the following signatures for SetStoreFunction interface;

(setter): void;
(k1, setter): void;
(k1, k2, setter): void;
(k1, k2, k3, setter): void;
(k1, k2, k3, k4, setter): void;
(k1, k2, k3, k4, k5, setter): void;
(k1, k2, k3, k4, k5, k6, setter): void;
(k1, k2, k3, k4, k5, k6, k7, setter): void;
(k1, k2, k3, k4, k5, k6, k7, ...rest): void;

I'm hidding the type because it's not relevant for the discussion. Said another way, at the moment, the interface only allow for:

  • exactly 1 arg
  • exactly 2 args
  • exactly 3 args
  • exactly 4 args
  • exactly 5 args
  • exactly 6 args
  • exactly 7 args
  • exactly 8 args
  • 8 or more args.

My proposition is to add overload for the cases:

  • 1 or more args
  • 2 or more args
  • 3 or more args
  • ...
  • 7 or more args

So that it would be legal to write :

const [, setUsers1] = createStore(
    [] as { id: number; firstName: string; lastName: Date; nickName: string; adresse: string }[]
  );
  const setUser1 = (id: number, ...rest: any) => setUsers1(user => user.id === id, ...rest);

Breaking change:

Adding support for those cases means that this does not error anymore

  const [, setStore] = createStore({ data: ["a", 1] as [string, number] });
  // Does Not Error anymore
  setStore("data", 0, 3);
  // Does Not Error anymore
  setStore("data", 1, "world");

The reason for that is that the rest of the overload 1+ typing is not smart enough to inspect the second argument so it thinks that he is looking at a (string | number)[]. I say it is not a big deal to lose this safety and it was a safety we didn't provide for case 8+ before my change anyway.

How did you test this change?

Commented the 2 failing tests
Added some test for the new signatures
pnpm test

@changeset-bot
Copy link

changeset-bot bot commented Feb 14, 2023

⚠️ No Changeset found

Latest commit: 3467074

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coveralls
Copy link

coveralls commented Feb 14, 2023

Pull Request Test Coverage Report for Build 4178913027

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 88.248%

Totals Coverage Status
Change from base Build 4172941315: 0.0%
Covered Lines: 1300
Relevant Lines: 1397

💛 - Coveralls

@SarguelUnda SarguelUnda marked this pull request as ready for review February 14, 2023 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants