From 45abcfaca979a20454ca0cf73095f153ff5ed6b7 Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 6 Apr 2024 11:33:42 +0900 Subject: [PATCH] refactor atomWithLazy --- src/vanilla/utils/atomWithLazy.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/vanilla/utils/atomWithLazy.ts b/src/vanilla/utils/atomWithLazy.ts index 208be7e547..7be481d852 100644 --- a/src/vanilla/utils/atomWithLazy.ts +++ b/src/vanilla/utils/atomWithLazy.ts @@ -3,10 +3,12 @@ import { PrimitiveAtom, atom } from '../../vanilla.ts' export function atomWithLazy( makeInitial: () => Value, ): PrimitiveAtom { - return { - ...atom(undefined as unknown as Value), - get init() { + const a = atom(undefined as unknown as Value) + delete (a as { init?: Value }).init + Object.defineProperty(a, 'init', { + get() { return makeInitial() }, - } + }) + return a }