Reatom with typescript
Reatom and Typescript
You don’t need to do anything, reatom have out of box typescript support. Inference works as you’d expect
// AtomMut<number>
const numAtom = atom(3)
// AtomMut<string>
const strAtom = atom('foo')
// Atom<string | number>
const dynamicAtom = atom((ctx) => {
const num = ctx.spy(numAtom)
const str = ctx.spy(strAtom)
return num > 0 ? num : str
})
It is recommended to use strict:true
, or strictNullChecks: true
in the tsconfig.json project for better experience
/* strictNullChecks: true */
// AtomMut<string | null>
const nullableAtom = atom<string | null>(null)
/* strictNullChecks: false */
// AtomMut<string>
const nullableAtom = atom<string | null>(null)
You can play with this example on typescript playground