Skip to content

Commit

Permalink
Merge pull request #6 from udecode/fix/provider-value-types
Browse files Browse the repository at this point in the history
Fix provider prop types
  • Loading branch information
Ziad Beyens authored Jan 5, 2024
2 parents c4724a4 + fedc959 commit cc77cf5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
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

0 comments on commit cc77cf5

Please sign in to comment.