reactjstypescriptrecoiljs

Recoil Atom and Typescript, define type and pass atom as parameter


I start to use recoils and typescript.

At this time, I use the default property to define each properties type of my atom :

 const WipStateAtom = atom({
    key: 'wipAtom',
    default: {
       data: null as IData | null,
       ex: null as IEx | null,
    }
 });

But is it possible to use an interface/or a type to define the atom content like :

export interface IWipAtom {
   data: IGameData | null,
   ex: IEx | null,
}

I want to pass an atom in a function but I don't want to type it as ANY :

const [wip, setWip] = useRecoilState(WipStateAtom);

const myFunction = (thewip: any) => { ... }

I prefer to strictly define its type :

const myFunction = (thewip: IWipAtom) => { ... }

Any way to type the atom ?


Solution

  • You can specify the type during the atom creation:

    const WipStateAtom = atom<IWipAtom>({
      key: 'wipAtom',
      default: {
        data: null as IData | null,
        ex: null as IEx | null,
      }
    });