import { SetStateAction } from 'react' ; export type Atom < Value > = { init ? : Value ; read : ( get : < V > ( a : Atom < V > ) => V ) => Value ; } ; export type WritableAtom < Value , Update > = Atom < Value > & { write : ( get : < V > ( a : Atom < V > ) => V , set : < V , U > ( a : WritableAtom < V , U > , u : U ) => void , update : Update ) => void ; } ; export type PrimitiveAtom < Value > = WritableAtom < Value , SetStateAction < Value > > ; export function atom < Value , Update > ( read , write ) ; export function atom < Value > ( read ) ; export function atom < Value > ( initialValue ) ; export function atom < Value , Update > ( read , write ? ) { if ( typeof read !== 'function' ) { const a = { init : read , read : ( get ) => get ( a ) , write : ( get , set , update ) => set ( a , typeof update === 'function' ? ( update as ( ( prev : Value ) => Value ) ) ( get ( a ) ) : update ) , } ; return a ; } if ( write ) { const a = { read : read as ( ( get : < V > ( _a : Atom < V > ) => V ) => Value ) , write , } ; return a ; } const a = { read : read as ( ( get : < V > ( _a : Atom < V > ) => V ) => Value ) , } ; return a ; }