diff --git a/packages/jotai-x/src/createAtomStore.spec.tsx b/packages/jotai-x/src/createAtomStore.spec.tsx index cac9073..5f4eb6c 100644 --- a/packages/jotai-x/src/createAtomStore.spec.tsx +++ b/packages/jotai-x/src/createAtomStore.spec.tsx @@ -407,7 +407,7 @@ describe('createAtomStore', () => { const derivedAtom = atom((get) => `My name is ${get(userStore.atom.name)}`); const DerivedAtomConsumer = () => { - const message = useUserStore().getAtom(derivedAtom); + const message = useUserStore().get.atom(derivedAtom); return
{message}
; }; diff --git a/packages/jotai-x/src/createAtomStore.ts b/packages/jotai-x/src/createAtomStore.ts index e96bc43..e59ab58 100644 --- a/packages/jotai-x/src/createAtomStore.ts +++ b/packages/jotai-x/src/createAtomStore.ts @@ -20,9 +20,17 @@ type UseAtomOptionsOrScope = UseAtomOptions | string; export type GetRecord = { [K in keyof O]: (options?: UseAtomOptionsOrScope) => O[K]; }; + +export type ExtendedGetRecord = GetRecord< + O & { + [key in keyof E]: E[key] extends Atom ? U : never; + } +>; + export type SetRecord = { [K in keyof O]: (options?: UseAtomOptionsOrScope) => (value: O[K]) => void; }; + export type UseRecord = { [K in keyof O]: ( options?: UseAtomOptionsOrScope @@ -75,16 +83,9 @@ type UseAtomFn = ( ) => [V, (...args: A) => R]; export type UseStoreApi = (options?: UseAtomOptionsOrScope) => { - get: GetRecord< - T & { - [key in keyof E]: E[key] extends Atom ? U : never; - } - >; - set: SetRecord; - use: UseRecord; - getAtom: GetAtomFn; - setAtom: SetAtomFn; - useAtom: UseAtomFn; + get: ExtendedGetRecord & { atom: GetAtomFn }; + set: SetRecord & { atom: SetAtomFn }; + use: UseRecord & { atom: UseAtomFn }; store: (options?: UseAtomOptionsOrScope) => JotaiStore | undefined; }; @@ -172,9 +173,9 @@ export const createAtomStore = < const useStoreIndex = getUseStoreIndex(name) as UseNameStore; const storeIndex = getStoreIndex(name) as NameStore; - const getAtoms = {} as ReturnType>['get']; - const setAtoms = {} as ReturnType>['set']; - const useAtoms = {} as ReturnType>['use']; + const getAtoms = {} as ExtendedGetRecord; + const setAtoms = {} as SetRecord; + const useAtoms = {} as UseRecord; const primitiveAtoms = {} as WritableAtomRecord; const useStore = (optionsOrScope: UseAtomOptionsOrScope = {}) => { @@ -236,24 +237,30 @@ export const createAtomStore = < }; const useStoreApi: UseStoreApi = (defaultOptions = {}) => ({ - get: withDefaultOptions(getAtoms, convertScopeShorthand(defaultOptions)), - set: withDefaultOptions(setAtoms, convertScopeShorthand(defaultOptions)), - use: withDefaultOptions(useAtoms, convertScopeShorthand(defaultOptions)), - getAtom: (atomConfig, options) => - useAtomValueWithStore(atomConfig, { - ...convertScopeShorthand(defaultOptions), - ...convertScopeShorthand(options), - }), - setAtom: (atomConfig, options) => - useSetAtomWithStore(atomConfig, { - ...convertScopeShorthand(defaultOptions), - ...convertScopeShorthand(options), - }), - useAtom: (atomConfig, options) => - useAtomWithStore(atomConfig, { - ...convertScopeShorthand(defaultOptions), - ...convertScopeShorthand(options), - }), + get: { + ...withDefaultOptions(getAtoms, convertScopeShorthand(defaultOptions)), + atom: (atomConfig, options) => + useAtomValueWithStore(atomConfig, { + ...convertScopeShorthand(defaultOptions), + ...convertScopeShorthand(options), + }), + }, + set: { + ...withDefaultOptions(setAtoms, convertScopeShorthand(defaultOptions)), + atom: (atomConfig, options) => + useSetAtomWithStore(atomConfig, { + ...convertScopeShorthand(defaultOptions), + ...convertScopeShorthand(options), + }), + }, + use: { + ...withDefaultOptions(useAtoms, convertScopeShorthand(defaultOptions)), + atom: (atomConfig, options) => + useAtomWithStore(atomConfig, { + ...convertScopeShorthand(defaultOptions), + ...convertScopeShorthand(options), + }), + }, store: (options) => useStore({ ...convertScopeShorthand(defaultOptions),