null/undefined initial atom value confuses typescript
See original GitHub issueWhenever I set null or undefined as the initial value on an atom, TypeScript infers the type as Atom
instead of WritableAtom
.
Working case: initial value is false
so type is shown as WritableAtom
:
Non-working case: initial value is null
so type is just Atom
:
This shows up as a type error on useUpdateAtom
:
My current workaround: caste to PrimitiveAtom
:
const myAtomConfig = atom<boolean | null>(null) as PrimitiveAtom<
boolean | null
>;
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:14 (7 by maintainers)
Top Results From Across the Web
Is there a way to check for both `null` and `undefined`?
Using a juggling-check, you can test both null and undefined in one hit: if (x == null) {. If you use a strict-check,...
Read more >Null Vs Undefined in TypeScript - TekTutorialsHub
TypeScript has two special values for Null and Undefined. Both represent no value or absence of any value. The difference between Null ...
Read more >How do you deal with null vs undefined? - DEV Community
A couple of thoughts: Is using undefined for initially undefined values and using null whenever you want to unassign a value a good...
Read more >Typescript: when to use null, undefined or empty array?
'null' is assigned to a variable to specify that the variable doesn't contain any value or is empty. But 'undefined' is used to...
Read more >The Non-Value Trio of JavaScript - Level Up Coding
Undefined, Null, and NaN are 3 keywords, known as non-values or empty values, that are often a source of buggy code, stress, and...
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
Sorry, it’s not explicit typing, but type assertion. Like OP noted:
Just FYI I ended up setting
struct: true
as well, but I think it would be good to know the workaround if possible! Thanks for being very responsive and I really enjoy usingjotai
so far!