async atomWithDefault shows Typescript error #2677
Answered
by
dai-shi
PhilipGrefe
asked this question in
Q&A
-
I'm searching for the easiest setup of an atom to:
The following code works, using atomWithDefault. However, typescript is complaining 🤷 const descriptionBaseAtom = atomWithDefault(async (get) => await db.query.someTable.findFirst({ columns: { description: true } }))
export const descriptionQueryAtom = atom(
(get) => get(descriptionBaseAtom),
(get, set, description: string) => {
set(descriptionBaseAtom, description)
// 👆 'description' shows 'Argument of type 'string' is not assignable to parameter of type 'unique symbol | SetStateAction<Promise<string | undefined>>.'
},
) Is there a way to use atomWithDefault properly typed? fyi the solution from https://x.com/dai_shi/status/1714126922542964899 works (without atomWithDefault). const asyncAtom = atom(async (get) => await db.query.someTable.findFirst({ columns: { description: true } }))
const defaultAtom = atom<string | undefined>(undefined)
export const finalAtom = atom(
(get) => get(defaultAtom) ?? get(asyncAtom),
(get, set, description: string) => {
set(defaultAtom, description)
},
) |
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Jul 28, 2024
Replies: 1 comment 1 reply
-
Check this out: https://tsplay.dev/WJk0gw import { atom } from "jotai";
import { atomWithDefault } from "jotai/utils";
const descriptionBaseAtom = atomWithDefault<
Promise<string | undefined> | string | undefined
>(async (get) => await Promise.resolve("foo" as string | undefined));
export const descriptionQueryAtom = atom(
(get) => get(descriptionBaseAtom),
(get, set, description: string) => {
set(descriptionBaseAtom, description);
},
); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
PhilipGrefe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check this out: https://tsplay.dev/WJk0gw