Skip to content

Commit

Permalink
add some overloads for StoreSetter
Browse files Browse the repository at this point in the history
  • Loading branch information
SarguelUnda committed Feb 14, 2023
1 parent e245736 commit 3467074
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
62 changes: 62 additions & 0 deletions packages/solid/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,68 @@ export interface SetStoreFunction<T> {
k7: Part<W<W<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>[K5]>[K6]>, K7>,
...rest: Rest<W<W<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>[K5]>[K6]>[K7], [K7, K6, K5, K4, K3, K2, K1]>
): void;

<
K1 extends KeyOf<W<T>>,
K2 extends KeyOf<W<W<T>[K1]>>,
K3 extends KeyOf<W<W<W<T>[K1]>[K2]>>,
K4 extends KeyOf<W<W<W<W<T>[K1]>[K2]>[K3]>>,
K5 extends KeyOf<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>>,
K6 extends KeyOf<W<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>[K5]>>
>(
k1: Part<W<T>, K1>,
k2: Part<W<W<T>[K1]>, K2>,
k3: Part<W<W<W<T>[K1]>[K2]>, K3>,
k4: Part<W<W<W<W<T>[K1]>[K2]>[K3]>, K4>,
k5: Part<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>, K5>,
k6: Part<W<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>[K5]>, K6>,
...rest: Rest<W<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>[K5]>[K6], [K6, K5, K4, K3, K2, K1]>
): void;

<
K1 extends KeyOf<W<T>>,
K2 extends KeyOf<W<W<T>[K1]>>,
K3 extends KeyOf<W<W<W<T>[K1]>[K2]>>,
K4 extends KeyOf<W<W<W<W<T>[K1]>[K2]>[K3]>>,
K5 extends KeyOf<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>>
>(
k1: Part<W<T>, K1>,
k2: Part<W<W<T>[K1]>, K2>,
k3: Part<W<W<W<T>[K1]>[K2]>, K3>,
k4: Part<W<W<W<W<T>[K1]>[K2]>[K3]>, K4>,
k5: Part<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>, K5>,
...rest: Rest<W<W<W<W<W<T>[K1]>[K2]>[K3]>[K4]>[K5], [K5, K4, K3, K2, K1]>
): void;

<
K1 extends KeyOf<W<T>>,
K2 extends KeyOf<W<W<T>[K1]>>,
K3 extends KeyOf<W<W<W<T>[K1]>[K2]>>,
K4 extends KeyOf<W<W<W<W<T>[K1]>[K2]>[K3]>>
>(
k1: Part<W<T>, K1>,
k2: Part<W<W<T>[K1]>, K2>,
k3: Part<W<W<W<T>[K1]>[K2]>, K3>,
k4: Part<W<W<W<W<T>[K1]>[K2]>[K3]>, K4>,
...rest: Rest<W<W<W<W<T>[K1]>[K2]>[K3]>[K4], [K4, K3, K2, K1]>
): void;

<K1 extends KeyOf<W<T>>, K2 extends KeyOf<W<W<T>[K1]>>, K3 extends KeyOf<W<W<W<T>[K1]>[K2]>>>(
k1: Part<W<T>, K1>,
k2: Part<W<W<T>[K1]>, K2>,
k3: Part<W<W<W<T>[K1]>[K2]>, K3>,
...rest: Rest<W<W<W<T>[K1]>[K2]>[K3], [K3, K2, K1]>
): void;

<K1 extends KeyOf<W<T>>, K2 extends KeyOf<W<W<T>[K1]>>>(
k1: Part<W<T>, K1>,
k2: Part<W<W<T>[K1]>, K2>,
...rest: Rest<W<W<T>[K1]>[K2], [K2, K1]>
): void;

<K1 extends KeyOf<W<T>>>(k1: Part<W<T>, K1>, ...rest: Rest<W<T>[K1], [K1]>): void;

(...rest: Rest<T, []>): void;
}

/**
Expand Down
58 changes: 56 additions & 2 deletions packages/solid/store/test/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,66 @@ describe("Nested Classes", () => {
const [, setStore] = createStore({ data: ["a", 1] as [string, number] });
setStore("data", 0, "hello");
setStore("data", 1, 2);
// @ts-expect-error number not assignable to string
// Not working anymore @ts-expect-error number not assignable to string
setStore("data", 0, 3);
// @ts-expect-error string not assignable to number
// Not working anymore @ts-expect-error string not assignable to number
setStore("data", 1, "world");
};

// can use a rest parameter as input of a setStore function in depth lower than 7
() => {
const [, setStore0] = createStore(
[] as { id: number; firstName: string; lastName: Date; nickName: string; adress: string }[]
);
const doSomethingThenSet = (...rest: any) => {
console.log(rest);
setStore0(...rest);
};

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

const [, setUsers2] = createStore({ a: { b: [] } } as {
a: { b: { id: number; data: string }[] };
});
const setUser2 = (id: number, ...rest: any) =>
setUsers2("a", "b", user => user.id === id, ...rest);

const [, setUsers3] = createStore({ a: { b: { c: [] } } } as {
a: { b: { c: { id: number; data: string }[] } };
});
const setUser3 = (id: number, ...rest: any) =>
setUsers3("a", "b", "c", user => user.id === id, ...rest);

const [, setUsers4] = createStore({ a: { b: { c: { d: [] } } } } as {
a: { b: { c: { d: { id: number; data: string }[] } } };
});
const setUser4 = (id: number, ...rest: any) =>
setUsers4("a", "b", "c", "d", user => user.id === id, ...rest);

const [, setUsers5] = createStore({ a: { b: { c: { d: { e: [] } } } } } as {
a: { b: { c: { d: { e: { id: number; data: string }[] } } } };
});
const setUser5 = (id: number, ...rest: any) =>
setUsers5("a", "b", "c", "d", "e", user => user.id === id, ...rest);

const [, setUsers6] = createStore({ a: { b: { c: { d: { e: { f: [] } } } } } } as {
a: { b: { c: { d: { e: { f: { id: number; data: string }[] } } } } };
});
const setUser6 = (id: number, ...rest: any) =>
setUsers6("a", "b", "c", "d", "e", "f", user => user.id === id, ...rest);

const [, setUsers7] = createStore({ a: { b: { c: { d: { e: { f: { g: [] } } } } } } } as {
a: { b: { c: { d: { e: { f: { g: { id: number; data: string }[] } } } } } };
});

const setUser7 = (id: number, ...rest: any) =>
// @ts-expect-error TODO ? 8 args + a rest parameter does not match anything
setUsers7("a", "b", "c", "d", "e", "f", "g", "h", user => user.id === id, ...rest);
};

// // cannot mutate a store directly
// () => {
// const [store, setStore] = createStore({ a: 1, nested: { a: 1 } });
Expand Down

0 comments on commit 3467074

Please sign in to comment.