Skip to content

Commit

Permalink
fix: add undefined initial value to primitive Atom definition
Browse files Browse the repository at this point in the history
  • Loading branch information
rtritto committed Jul 20, 2024
1 parent debff8b commit 57517fa
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/vanilla/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Write<Args extends unknown[], Result> = (
// This is an internal type and not part of public API.
// Do not depend on it as it can change without notice.
type WithInitialValue<Value> = {
init: Value
init: Value | undefined
}

type OnUnmount = () => void
Expand Down Expand Up @@ -86,14 +86,17 @@ export function atom<Value>(
initialValue: Value,
): PrimitiveAtom<Value> & WithInitialValue<Value>

// primitive atom without default value
export function atom<Value>(): PrimitiveAtom<Value> & WithInitialValue<Value>

export function atom<Value, Args extends unknown[], Result>(
read: Value | Read<Value, SetAtom<Args, Result>>,
read?: Value | Read<Value, SetAtom<Args, Result>>,
write?: Write<Args, Result>,
) {
const key = `atom${++keyCount}`
const config = {
toString: () => key,
} as WritableAtom<Value, Args, Result> & { init?: Value }
} as WritableAtom<Value, Args, Result> & WithInitialValue<Value>
if (typeof read === 'function') {
config.read = read as Read<Value, SetAtom<Args, Result>>
} else {
Expand Down

0 comments on commit 57517fa

Please sign in to comment.