Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atom state setter is of type never when initial value is null or undefined #838

Closed
nicmeriano opened this issue Nov 14, 2021 · 10 comments · Fixed by #1042
Closed

Atom state setter is of type never when initial value is null or undefined #838

nicmeriano opened this issue Nov 14, 2021 · 10 comments · Fixed by #1042
Labels
documentation Improvements or additions to documentation help wanted Please someone help on this

Comments

@nicmeriano
Copy link

When initializing an atom with null or undefined the returned setter has a type of never, which makes typescript unhappy whenever I try to invoke it to update the state.

// Works as expected - setCount: (update?: SetStateAction<number>) => void
const [count, setCount] = atom(0)

// Doesn't work as expected - setCount: never
const [count, setCount] = atom<number | null>(null);

I'd be happy to look into it if needed.

@dai-shi
Copy link
Member

dai-shi commented Nov 14, 2021

related: #802 and #550

I assume you are in the non-strict mode. (which isn't supported... unfortunately.)

See this for workaround: #802 (comment)

@nicmeriano
Copy link
Author

Ah yes, although not ideal enabling strictNullChecks does seem to solve the issue. Thanks!

@dai-shi
Copy link
Member

dai-shi commented Nov 15, 2021

Yeah, it would be nice if it can be supported without too much hustle. We have tried it in #550, but anyone can try if they want to be challenged.

Closing this, but this has been reported three times, so it's worth considering to add a note in docs.

@dai-shi dai-shi closed this as completed Nov 15, 2021
@dai-shi
Copy link
Member

dai-shi commented Nov 15, 2021

Maybe, leave it open for a while.

@dai-shi dai-shi reopened this Nov 15, 2021
@dai-shi dai-shi added the documentation Improvements or additions to documentation label Nov 15, 2021
@dai-shi dai-shi added the help wanted Please someone help on this label Feb 11, 2022
@phoenixeliot
Copy link

Came here cause I just spent 30min being very confused as to how to set the values of atoms, thinking I was doing it wrong.
Perhaps jotai could emit some kind of warning when using null? Not sure if that's feasible.

Either way, I would love a note added to the docs about this problem.

@dai-shi
Copy link
Member

dai-shi commented Mar 5, 2022

okay, let me work on adding a tiny note.

@HoltMansfield
Copy link

const [count, setCount] = atom(null as number)

This works for me with strict: false

@dai-shi
Copy link
Member

dai-shi commented Feb 10, 2023

Cool. Do you mean atom(null as number | null) work with strict: false?

@HoltMansfield
Copy link

Cool. Do you mean atom(null as number | null) work with strict: false?

The line I posted is the exact line I tried in my editor.

I'm not a TS expert by an means.

I can do this with strict:false

const selectedProjectAtom = atom<Project | null>(null as Project)

and

const selectedProjectAtom = atom<Project>(null as Project | null)

It appears both lines are equivalent.

@dulee-dev
Copy link

dulee-dev commented Feb 21, 2023

hey guys you should try this

  1. you need to write type for setValue like below

import { SetStateAction, useAtom } from 'jotai';
const init: T = ....
const [value, setValue] = useAtom<T, SetStateAction< T>, void>(init);

  1. I make the "useTypeAtom< T>" hook

const useTypeAtom = < T>(init: PrimitiveAtom< T>) => {
return useAtom<T, SetStateAction< T>, void>(init);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Please someone help on this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants