-
-
Notifications
You must be signed in to change notification settings - Fork 649
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
null/undefined initial atom value confuses typescript #550
Comments
Oh... Nice catch. Do you happen to know how to fix?? |
Looks like TypeScript is choosing the wrong overload for atom(). It is choosing between these two: export declare function atom<Value>(read: Read<Value>): Atom<Value>;
export declare function atom<Value>(initialValue: Value): [Value] extends [Function] ? never : PrimitiveAtom<Value> & WithInitialValue<Value>; When we have a non-null like But when we have I couldn't figure out how to tell TypeScript that export declare function atom<Value>(initialValue: null | undefined): [Value] extends [Function] ? never : PrimitiveAtom<Value> & WithInitialValue<Value>;
export declare function atom<Value>(read: Read<Value>): Atom<Value>;
export declare function atom<Value>(initialValue: Value): [Value] extends [Function] ? never : PrimitiveAtom<Value> & WithInitialValue<Value>; A little verbose but it works. I sent a PR. |
Hmm, I can't reproduce this. Oh, I guess your strictNullChecks is off? Proper typing would be not possible without strictNullChecks. |
Ah, yes you are correct. This issue only occurs with |
I do plan to enable
I also have about 10 dependencies that I use types for, and I'm not getting any errors from the other libraries, so I believe this should be possible. Unless Jotai is doing something totally unique here? Perhaps it is doing more overloading than most other libraries? In any case I'll see if I can convince you with a solution that is better than #544 - but probably not until next week. |
wow, really... Does it mean
If you try At the same time, I don't think the current atom types are ideal, so putting your effort here and doing some experiments might reveal something that can improve types even with |
I double checked and I was not correct. I ended up changing my project to |
Yes, I know. This is very unfortunate and annoying. Closing. Look forward to another issue report! |
Hi there, is there any way we can re-open this? I'm hoping we can use |
It’s closed because I think it’s impossible to fix in a reasonable way. But I might be wrong. Happy to consider suggestions from anyone. |
Can you give an example of what you mean? |
Just FYI I ended up setting |
Sorry, it's not explicit typing, but type assertion. const myAtomConfig = atom<boolean | null>(null) as PrimitiveAtom<
boolean | null
>; |
Whenever I set null or undefined as the initial value on an atom, TypeScript infers the type as
Atom
instead ofWritableAtom
.Working case: initial value is

false
so type is shown asWritableAtom
:Non-working case: initial value is
null
so type is justAtom
:This shows up as a type error on

useUpdateAtom
:My current workaround: caste to
PrimitiveAtom
:The text was updated successfully, but these errors were encountered: