reactjstypescriptrecoiljs

How to create default type in an atom using typescript?


I am trying to create an atom for an object. I get the following error under default in the atom: Type '{}' is not assignable to type 'Country | RecoilValue | Promise | Loadable | WrappedValue'. Property '[WrappedValue_OPAQUE]' is missing in type '{}' but required in type 'WrappedValue'.ts(2322) How can I fix this? I am new to typescript.

export interface Country {
  capital: string;
  continents: string;
}
import { Country } from "../types/country";
import { atom } from "recoil";

export const countryAtom = atom<Country>({
  key: "countryAtom",
  default: {},
});

Solution

  • You can specify the type during the atom creation:

    export interface Country {
      captial: string;
      continets: string;
    }
    
    export const countryAtom = atom<Country>({
      key: "countryAtom",
      default: {
        captial: "example string 1",
        continets: "example string 2",
      }
    });