Skip to content

Commit bdf39a7

Browse files
authored
refactor(store): improve store types (#763)
* refactor(store): improve store types store.ts `NotWrappable` now consists of all non-object/array types. Simplified `DeepReadonly<T>`. Simplified `StoreNode`. `Store<T>` is now an alias for `DeepReadonly<T>`. Refactored `Part`, `Next`, and `SetStoreFunction` to improve their performance. Changed `NullableNext` to `Next`, and `Next` to `WrappableNext`. `WrappableNext` excludes all non-wrappable types instead of just non-nullable types, so `{a: number | {b: number}}` now allows `setStore("a", "b", 1)`. Improved the rest overload for `SetStoreFunction<T>`: it now correctly infers all types in the correct order, except for `ArrayFilterFn`s which need to be explicitly typed. store.spec.ts Added a few type test cases for store types. modifiers.ts Improved types of `reconcile` and `produce`; the latter now correctly infers types. modifiers.spec.ts Allow `produce` calls to have their types inferred. server.ts Import types from store.ts instead of redefining them. mutable.ts Return `T` instead of `Store<T>`, as the latter is no longer writable. index.ts Removed the `Readonly` export. Added `WrappableNext`. fixes #732 * test(store): enable store tests and fix failing tests
1 parent 742d412 commit bdf39a7

8 files changed

Lines changed: 408 additions & 330 deletions

File tree

‎packages/solid/store/src/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export type {
99
ArrayFilterFn,
1010
Part,
1111
Next,
12-
Readonly,
12+
WrappableNext,
1313
DeepReadonly
1414
} from "./store";
1515
export * from "./mutable";
16-
export * from "./modifiers";
16+
export * from "./modifiers";

‎packages/solid/store/src/modifiers.ts‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
setProperty,
3-
unwrap,
4-
isWrappable,
5-
Store,
6-
NotWrappable,
7-
StoreNode,
8-
DeepReadonly,
9-
$RAW,
10-
} from "./store";
1+
import { setProperty, unwrap, isWrappable, Store, StoreNode, $RAW } from "./store";
112

123
export type ReconcileOptions = {
134
key?: string | null;
@@ -113,18 +104,15 @@ function applyState(
113104

114105
// Diff method for setState
115106
export function reconcile<T>(
116-
value: T | Store<T>,
107+
value: T,
117108
options: ReconcileOptions = {}
118-
): (
119-
state: T extends NotWrappable ? T : Store<DeepReadonly<T>>
120-
) => T extends NotWrappable ? T : Store<T> {
109+
): (state: unknown) => Store<T> {
121110
const { merge, key = "id" } = options,
122111
v = unwrap(value);
123-
return s => {
124-
const state = s as T extends NotWrappable ? T : Store<T>;
125-
if (!isWrappable(state) || !isWrappable(v)) return v as T extends NotWrappable ? T : Store<T>;
112+
return state => {
113+
if (!isWrappable(state) || !isWrappable(v)) return v as Store<T>;
126114
applyState(v, { state }, "state", merge, key);
127-
return state;
115+
return state as Store<T>;
128116
};
129117
}
130118

@@ -147,13 +135,8 @@ const setterTraps: ProxyHandler<StoreNode> = {
147135
};
148136

149137
// Immer style mutation style
150-
export function produce<T>(
151-
fn: (state: T) => void
152-
): (
153-
state: T extends NotWrappable ? T : Store<DeepReadonly<T>>
154-
) => T extends NotWrappable ? T : Store<T> {
155-
return s => {
156-
const state = s as T extends NotWrappable ? T : Store<T>;
138+
export function produce<T>(fn: (state: T) => void): (state: Store<T>) => Store<T> {
139+
return state => {
157140
if (isWrappable(state)) fn(new Proxy(state as object, setterTraps) as unknown as T);
158141
return state;
159142
};

‎packages/solid/store/src/mutable.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
$NODE,
99
$NAME,
1010
StoreNode,
11-
Store,
1211
setProperty,
1312
proxyDescriptor,
1413
ownKeys
@@ -52,7 +51,7 @@ const proxyTraps: ProxyHandler<StoreNode> = {
5251
getOwnPropertyDescriptor: proxyDescriptor
5352
};
5453

55-
function wrap<T extends StoreNode>(value: T, name?: string): Store<T> {
54+
function wrap<T extends StoreNode>(value: T, name?: string): T {
5655
let p = value[$PROXY];
5756
if (!p) {
5857
Object.defineProperty(value, $PROXY, { value: (p = new Proxy(value, proxyTraps)) });
@@ -79,10 +78,7 @@ function wrap<T extends StoreNode>(value: T, name?: string): Store<T> {
7978
return p;
8079
}
8180

82-
export function createMutable<T extends StoreNode>(
83-
state: T | Store<T>,
84-
options?: { name?: string }
85-
): Store<T> {
81+
export function createMutable<T extends StoreNode>(state: T, options?: { name?: string }): T {
8682
const unwrappedStore = unwrap<T>(state || {});
8783
const wrappedStore = wrap(
8884
unwrappedStore,
@@ -92,5 +88,5 @@ export function createMutable<T extends StoreNode>(
9288
const name = (options && options.name) || DEV.hashValue(unwrappedStore);
9389
DEV.registerGraph(name, { value: unwrappedStore });
9490
}
95-
return wrappedStore as Store<T>;
91+
return wrappedStore;
9692
}

‎packages/solid/store/src/server.ts‎

Lines changed: 5 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
export const $RAW = Symbol("state-raw");
2-
3-
// well-known symbols need special treatment until https://github.com/microsoft/TypeScript/issues/24622 is implemented.
4-
type AddSymbolToPrimitive<T> = T extends { [Symbol.toPrimitive]: infer V }
5-
? { [Symbol.toPrimitive]: V }
6-
: {};
7-
type AddCallable<T> = T extends { (...x: any[]): infer V } ? { (...x: Parameters<T>): V } : {};
1+
import { SetStoreFunction, Store } from "store";
82

9-
type NotWrappable = string | number | boolean | Function | null;
10-
export type Store<T> = {
11-
[P in keyof T]: T[P] extends object ? Store<T[P]> : T[P];
12-
} & {
13-
[$RAW]?: T;
14-
} & AddSymbolToPrimitive<T> &
15-
AddCallable<T>;
3+
export const $RAW = Symbol("state-raw");
164

175
export function isWrappable(obj: any) {
186
return (
@@ -87,118 +75,14 @@ export function updatePath(current: any, path: any[], traversed: (number | strin
8775
} else setProperty(current, part, value);
8876
}
8977

90-
type StoreSetter<T> =
91-
| Partial<T>
92-
| ((
93-
prevState: T extends NotWrappable ? T : Store<T>,
94-
traversed?: (string | number)[]
95-
) => Partial<T> | void);
96-
type StorePathRange = { from?: number; to?: number; by?: number };
97-
98-
type ArrayFilterFn<T> = (item: T extends any[] ? T[number] : never, index: number) => boolean;
99-
100-
type Part<T> = keyof T | Array<keyof T> | StorePathRange | ArrayFilterFn<T>; // changing this to "T extends any[] ? ArrayFilterFn<T> : never" results in depth limit errors
101-
102-
type Next<T, K> = K extends keyof T
103-
? T[K]
104-
: K extends Array<keyof T>
105-
? T[K[number]]
106-
: T extends any[]
107-
? K extends StorePathRange
108-
? T[number]
109-
: K extends ArrayFilterFn<T>
110-
? T[number]
111-
: never
112-
: never;
113-
114-
export interface SetStoreFunction<T> {
115-
<Setter extends StoreSetter<T>>(...args: [Setter]): void;
116-
<K1 extends Part<T>, Setter extends StoreSetter<Next<T, K1>>>(...args: [K1, Setter]): void;
117-
<
118-
K1 extends Part<T>,
119-
K2 extends Part<Next<T, K1>>,
120-
Setter extends StoreSetter<Next<Next<T, K1>, K2>>
121-
>(
122-
...args: [K1, K2, Setter]
123-
): void;
124-
<
125-
K1 extends Part<T>,
126-
K2 extends Part<Next<T, K1>>,
127-
K3 extends Part<Next<Next<T, K1>, K2>>,
128-
Setter extends StoreSetter<Next<Next<Next<T, K1>, K2>, K3>>
129-
>(
130-
...args: [K1, K2, K3, Setter]
131-
): void;
132-
<
133-
K1 extends Part<T>,
134-
K2 extends Part<Next<T, K1>>,
135-
K3 extends Part<Next<Next<T, K1>, K2>>,
136-
K4 extends Part<Next<Next<Next<T, K1>, K2>, K3>>,
137-
Setter extends StoreSetter<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>>
138-
>(
139-
...args: [K1, K2, K3, K4, Setter]
140-
): void;
141-
<
142-
K1 extends Part<T>,
143-
K2 extends Part<Next<T, K1>>,
144-
K3 extends Part<Next<Next<T, K1>, K2>>,
145-
K4 extends Part<Next<Next<Next<T, K1>, K2>, K3>>,
146-
K5 extends Part<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>>,
147-
Setter extends StoreSetter<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>>
148-
>(
149-
...args: [K1, K2, K3, K4, K5, Setter]
150-
): void;
151-
<
152-
K1 extends Part<T>,
153-
K2 extends Part<Next<T, K1>>,
154-
K3 extends Part<Next<Next<T, K1>, K2>>,
155-
K4 extends Part<Next<Next<Next<T, K1>, K2>, K3>>,
156-
K5 extends Part<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>>,
157-
K6 extends Part<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>>,
158-
Setter extends StoreSetter<Next<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>, K6>>
159-
>(
160-
...args: [K1, K2, K3, K4, K5, K6, Setter]
161-
): void;
162-
<
163-
K1 extends Part<T>,
164-
K2 extends Part<Next<T, K1>>,
165-
K3 extends Part<Next<Next<T, K1>, K2>>,
166-
K4 extends Part<Next<Next<Next<T, K1>, K2>, K3>>,
167-
K5 extends Part<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>>,
168-
K6 extends Part<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>>,
169-
K7 extends Part<Next<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>, K6>>,
170-
Setter extends StoreSetter<
171-
Next<Next<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>, K6>, K7>
172-
>
173-
>(
174-
...args: [K1, K2, K3, K4, K5, K6, K7, Setter]
175-
): void;
176-
177-
// and here we give up on being accurate after 8 args
178-
<
179-
K1 extends Part<T>,
180-
K2 extends Part<Next<T, K1>>,
181-
K3 extends Part<Next<Next<T, K1>, K2>>,
182-
K4 extends Part<Next<Next<Next<T, K1>, K2>, K3>>,
183-
K5 extends Part<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>>,
184-
K6 extends Part<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>>,
185-
K7 extends Part<Next<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>, K6>>,
186-
K8 extends Part<Next<Next<Next<Next<Next<Next<Next<T, K1>, K2>, K3>, K4>, K5>, K6>, K7>>
187-
>(
188-
...args: [K1, K2, K3, K4, K5, K6, K7, K8, ...(Part<any> | StoreSetter<any>)[]]
189-
): void;
190-
}
191-
19278
export function createStore<T>(state: T | Store<T>): [Store<T>, SetStoreFunction<T>] {
19379
function setStore(...args: any[]): void {
19480
updatePath(state, args);
19581
}
19682
return [state as Store<T>, setStore];
19783
}
19884

199-
export function createMutable<T>(
200-
state: T | Store<T>
201-
): Store<T> {
85+
export function createMutable<T>(state: T | Store<T>): Store<T> {
20286
return state as Store<T>;
20387
}
20488

@@ -211,7 +95,7 @@ type ReconcileOptions = {
21195
export function reconcile<T>(
21296
value: T | Store<T>,
21397
options: ReconcileOptions = {}
214-
): (state: T extends NotWrappable ? T : Store<T>) => void {
98+
): (state: Store<T>) => void {
21599
return state => {
216100
if (!isWrappable(state)) return value;
217101
const targetKeys = Object.keys(value) as (keyof T)[];
@@ -228,9 +112,7 @@ export function reconcile<T>(
228112
}
229113

230114
// Immer style mutation style
231-
export function produce<T>(
232-
fn: (state: T) => void
233-
): (state: T extends NotWrappable ? T : Store<T>) => T extends NotWrappable ? T : Store<T> {
115+
export function produce<T>(fn: (state: T) => void): (state: Store<T>) => Store<T> {
234116
return state => {
235117
if (isWrappable(state)) fn(state as T);
236118
return state;

0 commit comments

Comments
 (0)