Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix provider prop types #6

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-bees-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jotai-x': patch
---

Fix: Provider prop types expect atoms instead of values for stores created with custom atoms
12 changes: 11 additions & 1 deletion packages/jotai-x/src/createAtomStore.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ describe('createAtomStore', () => {
isCustomAtom: true,
});

const { customStore } = createAtomStore(
const { customStore, useCustomStore, CustomProvider } = createAtomStore(
{
x: createCustomAtom(1),
},
Expand All @@ -496,6 +496,16 @@ describe('createAtomStore', () => {
const myAtom = customStore.atom.x as CustomAtom<number>;
expect(myAtom.isCustomAtom).toBe(true);
});

it('accepts initial values', () => {
const { result } = renderHook(() => useCustomStore().get.x(), {
wrapper: ({ children }) => (
<CustomProvider x={2}>{children}</CustomProvider>
),
});

expect(result.current).toBe(2);
});
});

describe('arbitrary atom accessors', () => {
Expand Down
20 changes: 14 additions & 6 deletions packages/jotai-x/src/createAtomStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type StoreAtomsWithoutExtend<T> = {
[K in keyof T]: T[K] extends Atom<any> ? T[K] : SimpleWritableAtom<T[K]>;
};

type ValueTypesForAtoms<T> = {
[K in keyof T]: T[K] extends Atom<infer V> ? V : never;
};

type StoreInitialValues<T> = ValueTypesForAtoms<StoreAtomsWithoutExtend<T>>;

type StoreAtoms<T, E> = StoreAtomsWithoutExtend<T> & E;

type FilterWritableAtoms<T> = {
Expand Down Expand Up @@ -108,7 +114,9 @@ export type AtomStoreApi<
> = {
name: N;
} & {
[key in keyof Record<NameProvider<N>, object>]: FC<ProviderProps<T>>;
[key in keyof Record<NameProvider<N>, object>]: FC<
ProviderProps<StoreInitialValues<T>>
>;
} & {
[key in keyof Record<NameStore<N>, object>]: StoreApi<T, E, N>;
} & {
Expand Down Expand Up @@ -185,6 +193,7 @@ export const createAtomStore = <
type MyStoreAtomsWithoutExtend = StoreAtomsWithoutExtend<T>;
type MyWritableStoreAtomsWithoutExtend =
FilterWritableAtoms<MyStoreAtomsWithoutExtend>;
type MyStoreInitialValues = StoreInitialValues<T>;

const providerIndex = getProviderIndex(name) as NameProvider<N>;
const useStoreIndex = getUseStoreIndex(name) as UseNameStore<N>;
Expand Down Expand Up @@ -278,11 +287,10 @@ export const createAtomStore = <
}
}

const Provider: FC<ProviderProps<T>> = createAtomProvider(
name,
writableAtomsWithoutExtend,
{ effect }
);
const Provider: FC<ProviderProps<MyStoreInitialValues>> = createAtomProvider<
MyStoreInitialValues,
N
>(name, writableAtomsWithoutExtend, { effect });

const storeApi: StoreApi<T, E, N> = {
atom: atoms,
Expand Down