question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

atomFamily missing Typescript types for function setter

See original GitHub issue

With 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: Screen Shot 2021-06-23 at 3 26 29 PM

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Jun 25, 2021

@ahfarmer Thanks for the repro!

Here is the fix:

const myAtomFamily = atomFamily(() =>
  atom<boolean>(false)
);

Please give it a try.

If you want to explicitly annotate it, try this:

const myAtomFamily = atomFamily<string, boolean, SetStateAction<boolean>>(() =  >
  atom<boolean>(false)                                                        
);                                                                            
0reactions
ahfarmercommented, Jun 25, 2021

Ahhh, wonderful. Thank you! I’ll go ahead and close this then.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found