Skip to content

Commit 6e59366

Browse files
committed
Provider handles multi-schema store
1 parent f1c98cf commit 6e59366

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/react.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88
import { getIdFromAction } from "./action.js";
99
import type { ThunkAction } from "./query/index.js";
1010
import {
11+
type DefaultSchemaKey,
1112
type FxSchema,
1213
type FxStore,
1314
PERSIST_LOADER_ID,
15+
type StoreSchemaRegistry,
1416
} from "./store/index.js";
1517
import type { LoaderOutput } from "./store/slice/loaders.js";
1618
import type { TableOutput } from "./store/slice/table.js";
@@ -180,9 +182,12 @@ export function createTypedHooks<O extends FxMap>(
180182
* }
181183
* ```
182184
*/
183-
export function Provider<O extends FxMap>(props: {
184-
store: FxStore<O>;
185-
schema?: FxSchema<O>;
185+
export function Provider<
186+
O extends FxMap,
187+
TSchemas extends StoreSchemaRegistry = StoreSchemaRegistry<FxSchema<O>>,
188+
>(props: {
189+
store: FxStore<O, TSchemas>;
190+
schema?: TSchemas[DefaultSchemaKey];
186191
children?: React.ReactNode;
187192
}): React.ReactElement;
188193

src/test/react-types.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expectTypeOf, test } from "vitest";
2+
import type { ReactElement } from "react";
23
import type { ThunkAction } from "../query/index.js";
34
import {
5+
Provider,
46
type UseApiAction,
57
type UseApiProps,
68
type UseApiSimpleProps,
@@ -139,6 +141,22 @@ describe("react hook types", () => {
139141
expectTypeOf(useSchemaState).returns.toEqualTypeOf<typeof schema>();
140142
expectTypeOf(useTypedLoader).returns.toEqualTypeOf<LoaderState>();
141143
});
144+
145+
test("Provider accepts stores with merged root state and narrower default schema", () => {
146+
const baseSchema = createSchema({
147+
users: slice.table<User>(),
148+
});
149+
const metadataSchema = createSchema({
150+
metadata: slice.obj<Metadata>({}),
151+
});
152+
const store = createStore({
153+
schema: { default: baseSchema, metadata: metadataSchema },
154+
});
155+
156+
const renderProvider = () => Provider({ store, children: null });
157+
158+
expectTypeOf(renderProvider).returns.toEqualTypeOf<ReactElement>();
159+
});
142160
});
143161

144162
describe("direct hooks with explicit schema generics", () => {

0 commit comments

Comments
 (0)