Skip to content

Commit da87e9b

Browse files
GabbeVryansolid
authored andcommitted
refactor(signals): preserve supplied store types
1 parent 2d444c3 commit da87e9b

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@solidjs/signals": patch
3+
"solid-js": patch
4+
---
5+
6+
Preserve the supplied type in `Store<T>` instead of adding a shallow readonly mapping.

‎packages/signals/src/store/store.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { Refreshable } from "../core/index.js";
44
import { GlobalQueue } from "../core/scheduler.js";
55
import { storeNextLookup } from "./next/target.js";
66

7-
/** A read-only view of a store's value as seen by consumers. Mutate it via the paired `StoreSetter`. */
8-
export type Store<T> = Readonly<T>;
7+
/** A reactive view of a store's value. Update it through the paired `StoreSetter`. */
8+
export type Store<T> = T;
99
/**
1010
* A store setter. The callback receives a writable **draft** of the store.
1111
*

‎packages/signals/tests/store/createStore.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ describe("State immutability", () => {
2525
test("Setting a property", () => {
2626
const [state] = createStore({ name: "John" });
2727
expect(state.name).toBe("John");
28-
// @ts-expect-error can't write readonly property
2928
state.name = "Jake";
3029
expect(state.name).toBe("John");
3130
});

‎packages/signals/tests/store/store.type-tests.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,40 @@ import {
1212
const [store, setStore] = createStore({ name: "John", age: 30 });
1313
store.name satisfies string;
1414
store.age satisfies number;
15-
// @ts-expect-error readonly
16-
store.name = "Jake";
15+
}
16+
17+
// ── stores preserve the supplied type ────────────────────────────────
18+
19+
{
20+
const [source] = createStore([{ id: 1 }]);
21+
const [store, setStore] = createStore(source);
22+
setStore(draft => {
23+
draft.push({ id: 2 });
24+
});
25+
store satisfies { id: number }[];
26+
}
27+
28+
{
29+
type List = { readonly id: string; items: { title: string }[] };
30+
const [list] = createStore<List>({ id: "todos", items: [] });
31+
const [store, setStore] = createStore({ list });
32+
setStore(draft => {
33+
draft.list.items = [{ title: "Review PR" }];
34+
// @ts-expect-error User-authored readonly remains readonly through nesting.
35+
draft.list.id = "done";
36+
});
37+
store satisfies { list: List };
38+
}
39+
40+
{
41+
type State = { readonly id: number; name: string };
42+
const [store, setStore] = createStore<State>({ id: 1, name: "John" });
43+
store satisfies State;
44+
setStore(draft => {
45+
draft.name = "Jane";
46+
// @ts-expect-error User-authored readonly properties remain readonly.
47+
draft.id = 2;
48+
});
1749
}
1850

1951
// ── createStore (projection) — seed matches return type ───────────────

0 commit comments

Comments
 (0)