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.

null/undefined initial atom value confuses typescript

See original GitHub issue

Whenever 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: Screen Shot 2021-06-23 at 3 12 58 PM

Non-working case: initial value is null so type is just Atom:

Screen Shot 2021-06-23 at 3 13 22 PM

This shows up as a type error on useUpdateAtom: Screen Shot 2021-06-23 at 3 13 15 PM

My current workaround: caste to PrimitiveAtom:

const myAtomConfig = atom<boolean | null>(null) as PrimitiveAtom<
  boolean | null
>;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Jul 11, 2021

The workaround for you should be explicit typing.

Can you give an example of what you mean?

Sorry, it’s not explicit typing, but type assertion. Like OP noted:

const myAtomConfig = atom<boolean | null>(null) as PrimitiveAtom<
  boolean | null
>;
0reactions
alexluongcommented, Jul 11, 2021

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 using jotai so far!

Read more comments on GitHub >

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

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