atomFamily missing Typescript types for function setter
See original GitHub issueWith regular atoms, we can have function setters. It works great and the Typescript types are working:
const updateAtom = useUpdateAtom(atomConfig)
...
updateAtom(prevValue => !prevValue);
The same functionality works with atomFamily, but the Typescript types for the function setter seem to be missing and we get “Argument of type () => … is not assignable to parameter of type …”. Here’s a full example:
import { atom, PrimitiveAtom } from "jotai";
import { atomFamily, useAtomValue, useUpdateAtom } from "jotai/utils";
import { useCallback } from "react";
const myAtomFamily = atomFamily<string, boolean | null, boolean | null>(
() => atom<boolean | null>(false)
);
export default function App() {
const atomValue = useAtomValue(myAtomFamily("meow"));
const updateAtom = useUpdateAtom(myAtomFamily("meow"));
const toggle = useCallback(() => {
updateAtom((x) => !x);
}, [updateAtom]);
return (
<div className="App">
<button onClick={toggle}>Toggle</button>
<p>Is on?: {atomValue ? "YES" : "NO"}</p>
</div>
);
}
Here’s a screenshot of the TypeScript error:
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
null/undefined initial atom value confuses typescript · Issue #550
Non-working case: initial value is null so type is just Atom : ... atomFamily missing Typescript types for function setter #551.
Read more >Missing typings in return type of function - Stack Overflow
I have problem with function createRouting which should return actually the same as it gets. But currently it return the simplest ...
Read more >atom(options) - Recoil
The atom() function returns a writeable RecoilState object. ... wrapped value, or another atom or selector of the same type representing the default...
Read more >Jotai vs. Recoil: What are the differences? - LogRocket Blog
Jotai and Recoil both have their benefits. This article compares the state management libraries to help you decide what's best for you.
Read more >atomFamily — Jotai, primitive and flexible state management ...
The atom family types will be inferred from initializeAtom. ... the type of parameter, value, and atom's setState function using TypeScript generics.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ahfarmer Thanks for the repro!
Here is the fix:
Please give it a try.
If you want to explicitly annotate it, try this:
Ahhh, wonderful. Thank you! I’ll go ahead and close this then.