Search code examples
reactjstypescriptrecoiljs

Recoil atom typescript default undefined


I'm new to Recoil and am now having a problem with it. The problem is that I would like to be able to define the atoms default value as undefined as I have to do some async calls and more to set it at the beginning, and sometimes it may be undefined until I get some user-input. So I am trying to define the atom as:

export const locationState: RecoilState<LocationInterface | undefined> = atom({
  key: "location",
  default: undefined,
});

But I am getting the following error: "Type 'RecoilState' is not assignable to type 'RecoilState<LocationInterface | undefined>'."

Everything is working elsewhere where I am using the recoilvalue, but I am getting the above error only in the atom file.

Is there a way to do what I am doing or am I trying to do something fundamentally wrong with Recoil?

Thanks in advance!


Solution

  • I solved it! The correct syntax is:

    export const locationState = atom<LocationInterface | undefined>({
      key: "location",
      default: undefined,
    });