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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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