Skip to content

Commit d4cac8a

Browse files
committed
update AtomOnInit type
1 parent 474f5da commit d4cac8a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/vanilla/internals.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Internal functions (subject to change without notice)
22
// In case you rely on them, be sure to pin the version
33

4-
import type { Atom, WritableAtom } from './atom.ts'
4+
import { type Atom, type WritableAtom } from './atom.ts'
55

66
type AnyValue = unknown
77
type AnyError = unknown
@@ -11,6 +11,7 @@ type OnUnmount = () => void
1111
type Getter = Parameters<AnyAtom['read']>[0]
1212
type Setter = Parameters<AnyWritableAtom['write']>[1]
1313
type EpochNumber = number
14+
type WithRequired<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> }
1415

1516
/**
1617
* Mutable atom state,
@@ -95,10 +96,9 @@ type AtomWrite = <Value, Args extends unknown[], Result>(
9596
type AtomOnInit = <Value>(store: Store, atom: Atom<Value>) => void
9697
type AtomOnMount = <Value, Args extends unknown[], Result>(
9798
store: Store,
98-
atom: WritableAtom<Value, Args, Result>,
99+
atom: WithRequired<WritableAtom<Value, Args, Result>, 'onMount'> ,
99100
setAtom: (...args: Args) => Result,
100101
) => OnUnmount | void
101-
102102
type EnsureAtomState = <Value>(
103103
store: Store,
104104
atom: Atom<Value>,
@@ -236,6 +236,10 @@ function isActuallyWritableAtom(atom: AnyAtom): atom is AnyWritableAtom {
236236
return !!(atom as AnyWritableAtom).write
237237
}
238238

239+
function hasOnMount<Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>): atom is WithRequired<WritableAtom<Value, Args, Result>, 'onMount'> {
240+
return !!atom.onMount
241+
}
242+
239243
function isAtomStateInitialized<Value>(atomState: AtomState<Value>): boolean {
240244
return 'v' in atomState || 'e' in atomState
241245
}
@@ -837,7 +841,7 @@ const BUILDING_BLOCK_mountAtom: MountAtom = (store, atom) => {
837841
t: new Set(),
838842
}
839843
mountedMap.set(atom, mounted)
840-
if (isActuallyWritableAtom(atom) && atom.onMount) {
844+
if (isActuallyWritableAtom(atom) && hasOnMount(atom)) {
841845
const processOnMount = () => {
842846
let isSync = true
843847
const setAtom = (...args: unknown[]) => {

0 commit comments

Comments
 (0)