Skip to content

Commit 88d1020

Browse files
committed
feat(store): improve store types
`Store<T>` provided quite a complicated type for no benefit to the user, and most internal functions used `StoreNode`, hence `Store<T>` is now an alias for `DeepReadonly<T>`. `DeepReadonly<T>` was simplified since afaict there was no reason for it to be as complicated as it was. The Setter type is mostly unchanged externally, except: - Only `number` is allowed to index Array types (before, for example Array methods and properties were allowed). - The fallback overload is now much better and correctly constrains types but requires type annotations (they can't be inferred). - All `Part`s are now typed with readonly as well (they should be I think?). - I tried to optimize the Setter as much as possible, and I think it's better at the same number of overloads but don't have any definitive proof of that. `produce` is now able to infer types correctly from the `setStore` call. fixes solidjs#732
1 parent cc2f8e5 commit 88d1020

6 files changed

Lines changed: 175 additions & 200 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export { createStore, unwrap, $RAW } from "./store";
22
export type {
33
Store,
4-
SetStoreFunction,
54
NotWrappable,
65
StoreNode,
76
StoreSetter,
87
StorePathRange,
98
ArrayFilterFn,
109
Part,
11-
Next,
12-
Readonly,
13-
DeepReadonly
10+
DeepReadonly,
11+
StoreKeys,
12+
StoreBranch,
13+
SetStoreFallback
1414
} from "./store";
1515
export * from "./mutable";
16-
export * from "./modifiers";
16+
export * from "./modifiers";

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

Lines changed: 16 additions & 24 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, NotWrappable } from "./store";
112

123
export type ReconcileOptions = {
134
key?: string | null;
@@ -113,16 +104,13 @@ 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: Store<T>) => 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);
127115
return state;
128116
};
@@ -146,15 +134,19 @@ const setterTraps: ProxyHandler<StoreNode> = {
146134
}
147135
};
148136

137+
type DeepMutable<T> = T extends NotWrappable
138+
? T
139+
: {
140+
-readonly [K in keyof T]: DeepMutable<T[K]>;
141+
};
142+
149143
// Immer style mutation style
150144
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>;
157-
if (isWrappable(state)) fn(new Proxy(state as object, setterTraps) as unknown as T);
145+
fn: (state: DeepMutable<T>) => void
146+
): (state: Store<T>) => Partial<Store<T>> {
147+
return state => {
148+
if (isWrappable(state))
149+
fn(new Proxy(state as object, setterTraps) as unknown as DeepMutable<T>);
158150
return state;
159151
};
160152
}

‎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
}

0 commit comments

Comments
 (0)