diff --git a/.changeset/fix-create-projection-seed-inference.md b/.changeset/fix-create-projection-seed-inference.md new file mode 100644 index 000000000..2b97fdca4 --- /dev/null +++ b/.changeset/fix-create-projection-seed-inference.md @@ -0,0 +1,6 @@ +--- +"@solidjs/signals": patch +"solid-js": patch +--- + +Fix `createProjection` seed typing so readonly store seeds do not override inference from the projection function return type. diff --git a/packages/solid-signals/src/store/projection.ts b/packages/solid-signals/src/store/projection.ts index 3efd2c3a2..44f739ee9 100644 --- a/packages/solid-signals/src/store/projection.ts +++ b/packages/solid-signals/src/store/projection.ts @@ -17,6 +17,7 @@ import { STORE_WRAP, storeSetter, storeTraps, + type NoFn, type ProjectionOptions, type Store } from "./store.js"; @@ -107,7 +108,7 @@ export function createProjectionInternal( */ export function createProjection( fn: (draft: T) => void | T | Promise | AsyncIterable, - seed: Partial, + seed: Partial | Store>, options?: ProjectionOptions ): Refreshable> { return createProjectionInternal(fn, seed, options).store; diff --git a/packages/solid-signals/tests/store/store.type-tests.ts b/packages/solid-signals/tests/store/store.type-tests.ts index 4a6cb2ecc..c1438ac00 100644 --- a/packages/solid-signals/tests/store/store.type-tests.ts +++ b/packages/solid-signals/tests/store/store.type-tests.ts @@ -98,6 +98,21 @@ import { store[0].name satisfies string; } +// ── createProjection — store as seed ────────────────────────────────── + +{ + const [todos] = createStore([] as { id: number; done: boolean }[]); + const proj = createProjection(() => todos.filter(t => !t.done), todos); + proj[0].id satisfies number; + proj[0].done satisfies boolean; +} + +{ + const [state] = createStore({ count: 0 }); + const proj = createProjection(() => ({ count: 1 }), state); + proj.count satisfies number; +} + // ── createOptimisticStore (projection) — partial seed ───────────────── { diff --git a/packages/solid/src/client/hydration.ts b/packages/solid/src/client/hydration.ts index 9acb5ac27..2d2455894 100644 --- a/packages/solid/src/client/hydration.ts +++ b/packages/solid/src/client/hydration.ts @@ -1017,7 +1017,7 @@ export const createOptimistic: { */ export const createProjection: ( fn: (draft: T) => void | T | Promise | AsyncIterable, - initialValue: T, + initialValue: Partial | Store>, options?: ProjectionOptions ) => Refreshable> = ((...args: any[]) => (_createProjection || coreProjection)(...args)) as any; diff --git a/packages/solid/src/server/signals.ts b/packages/solid/src/server/signals.ts index ae203f7e6..122893f09 100644 --- a/packages/solid/src/server/signals.ts +++ b/packages/solid/src/server/signals.ts @@ -1056,7 +1056,7 @@ function createPendingProxy( export function createProjection( fn: (draft: T) => void | T | Promise | AsyncIterable, - initialValue: Partial, + initialValue: Partial | Store, options?: ServerSsrOptions ): Store { const ctx = sharedConfig.context;