From 99c5e715fb2b76ade4904b6f3e01d79d0f7e23b6 Mon Sep 17 00:00:00 2001 From: mac Date: Sun, 17 Dec 2023 21:21:46 +0900 Subject: [PATCH 1/3] fix: add init property to the return type of atomWithReset --- src/vanilla.ts | 7 ++++++- src/vanilla/atom.ts | 2 +- src/vanilla/utils/atomWithReset.ts | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/vanilla.ts b/src/vanilla.ts index 3918c93f6e..ee181ae620 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -1,5 +1,10 @@ export { atom } from './vanilla/atom.ts' -export type { Atom, WritableAtom, PrimitiveAtom } from './vanilla/atom.ts' +export type { + Atom, + WritableAtom, + PrimitiveAtom, + WithInitialValue, +} from './vanilla/atom.ts' export { createStore, getDefaultStore } from './vanilla/store.ts' export type { Getter, diff --git a/src/vanilla/atom.ts b/src/vanilla/atom.ts index 587705b4f6..650e18a9d2 100644 --- a/src/vanilla/atom.ts +++ b/src/vanilla/atom.ts @@ -23,7 +23,7 @@ type Write = ( ...args: Args ) => Result -type WithInitialValue = { +export type WithInitialValue = { init: Value } diff --git a/src/vanilla/utils/atomWithReset.ts b/src/vanilla/utils/atomWithReset.ts index 50ce8fe587..4a56a1afce 100644 --- a/src/vanilla/utils/atomWithReset.ts +++ b/src/vanilla/utils/atomWithReset.ts @@ -1,5 +1,5 @@ import { atom } from '../../vanilla.ts' -import type { WritableAtom } from '../../vanilla.ts' +import type { WithInitialValue, WritableAtom } from '../../vanilla.ts' import { RESET } from './constants.ts' type SetStateActionWithReset = @@ -20,5 +20,5 @@ export function atomWithReset(initialValue: Value) { set(anAtom, nextValue === RESET ? initialValue : nextValue) }, ) - return anAtom as WritableAtom + return anAtom as WritableAtom & WithInitialValue } From 2af6ed6feb0731c2ae172fb573859237d9d0edfd Mon Sep 17 00:00:00 2001 From: mac Date: Mon, 18 Dec 2023 00:02:24 +0900 Subject: [PATCH 2/3] fix: modify to not export WithInitialValue from atom.ts, re define it in atomWithReset.ts --- src/vanilla.ts | 7 +------ src/vanilla/atom.ts | 2 +- src/vanilla/utils/atomWithReset.ts | 6 +++++- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/vanilla.ts b/src/vanilla.ts index ee181ae620..3918c93f6e 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -1,10 +1,5 @@ export { atom } from './vanilla/atom.ts' -export type { - Atom, - WritableAtom, - PrimitiveAtom, - WithInitialValue, -} from './vanilla/atom.ts' +export type { Atom, WritableAtom, PrimitiveAtom } from './vanilla/atom.ts' export { createStore, getDefaultStore } from './vanilla/store.ts' export type { Getter, diff --git a/src/vanilla/atom.ts b/src/vanilla/atom.ts index 650e18a9d2..587705b4f6 100644 --- a/src/vanilla/atom.ts +++ b/src/vanilla/atom.ts @@ -23,7 +23,7 @@ type Write = ( ...args: Args ) => Result -export type WithInitialValue = { +type WithInitialValue = { init: Value } diff --git a/src/vanilla/utils/atomWithReset.ts b/src/vanilla/utils/atomWithReset.ts index 4a56a1afce..5999d24a8b 100644 --- a/src/vanilla/utils/atomWithReset.ts +++ b/src/vanilla/utils/atomWithReset.ts @@ -1,5 +1,5 @@ import { atom } from '../../vanilla.ts' -import type { WithInitialValue, WritableAtom } from '../../vanilla.ts' +import type { WritableAtom } from '../../vanilla.ts' import { RESET } from './constants.ts' type SetStateActionWithReset = @@ -7,6 +7,10 @@ type SetStateActionWithReset = | typeof RESET | ((prev: Value) => Value | typeof RESET) +type WithInitialValue = { + init: Value +} + export function atomWithReset(initialValue: Value) { type Update = SetStateActionWithReset const anAtom = atom( From dcd853a17678bbea6fea9495ed1a1ca8231cacc4 Mon Sep 17 00:00:00 2001 From: daishi Date: Mon, 18 Dec 2023 07:23:31 +0900 Subject: [PATCH 3/3] add comments --- src/vanilla/atom.ts | 2 ++ src/vanilla/utils/atomWithReset.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/vanilla/atom.ts b/src/vanilla/atom.ts index 587705b4f6..82b09958a4 100644 --- a/src/vanilla/atom.ts +++ b/src/vanilla/atom.ts @@ -23,6 +23,8 @@ type Write = ( ...args: Args ) => 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 = { init: Value } diff --git a/src/vanilla/utils/atomWithReset.ts b/src/vanilla/utils/atomWithReset.ts index 5999d24a8b..8dff76b7e5 100644 --- a/src/vanilla/utils/atomWithReset.ts +++ b/src/vanilla/utils/atomWithReset.ts @@ -7,6 +7,8 @@ type SetStateActionWithReset = | typeof RESET | ((prev: Value) => Value | typeof RESET) +// This is an internal type and not part of public API. +// Do not depend on it as it can change without notice. type WithInitialValue = { init: Value }