solid icon indicating copy to clipboard operation
solid copied to clipboard

Feat: add some overloads for StoreSetter

Open SarguelUnda opened this issue 2 years ago • 2 comments

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

SarguelUnda avatar Feb 14 '23 23:02 SarguelUnda

⚠️ No Changeset found

Latest commit: 346707464d5455df74348786b64d909b46cba22a

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

changeset-bot[bot] avatar Feb 14 '23 23:02 changeset-bot[bot]

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

coveralls avatar Feb 14 '23 23:02 coveralls