Skip to content

Commit

Permalink
chore: type cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Sep 20, 2024
1 parent 02cee1b commit 278e96a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare interface ImportMeta {
env?: {
MODE: string
}
}
6 changes: 3 additions & 3 deletions src/useAtomValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createDeepSignal } from './createDeepSignal'
import { useStore } from './Provider'
import type { AwaitedAccessorOrResource } from './useAtom'

function isPromise(x: unknown): x is Promise<unknown> {
return x instanceof Promise
function isPromiseLike(x: unknown): x is PromiseLike<unknown> {
return typeof (x as any)?.then === 'function'
}

type Store = ReturnType<typeof useStore>
Expand All @@ -29,7 +29,7 @@ export function useAtomValue<Value>(atom: Atom<Value>, options?: Options) {
const store = useStore(options)
const initial = store.get(atom)

if (isPromise(initial)) {
if (isPromiseLike(initial)) {
const [atomValue, { refetch }] = createResource(() => store.get(atom), { storage: createDeepSignal })

const unsub = store.sub(atom, () => refetch())
Expand Down
3 changes: 1 addition & 2 deletions src/useSetAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export function useSetAtom<Value, Args extends unknown[], Result>(
) {
const store = useStore(options)
function setAtom(...args: Args) {
// @ts-expect-error: TODO meta types
if (import.meta.env.DEV && !('write' in atom)) {
if (import.meta.env?.MODE !== 'production' && !('write' in atom)) {
// useAtom can pass non writable atom with wrong type assertion,
// so we should check here.
throw new Error('not writable atom')
Expand Down

0 comments on commit 278e96a

Please sign in to comment.