Skip to content

null/undefined initial atom value confuses typescript #550

@andrewagain

Description

@andrewagain

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions