import { A , pipe , R } from '../..' ; import { expectType } from 'ts-expect' ; describe ( 'catchError' , ( ) => { it ( 'provides correct types' , ( ) => { expectType < R . Result < string , string > > ( pipe ( R . fromNullable ( null , 'this is bad' ) , R . catchError ( _ => R . Ok ( 'string' ) ) ) ) ; expectType < R . Result < string , number > > ( pipe ( R . fromNullable ( null , 'this is bad' ) , R . catchError ( _ => R . Error ( 0 ) ) ) ) ; } ) ; it ( 'catches error' , ( ) => { expect ( pipe ( R . fromNullable ( [ 1 , 2 , 3 ] , 'this is bad' ) , R . catchError ( _ => R . Ok ( A . makeEmpty < number > ( ) ) ) ) ) . toEqual ( R . Ok ( [ 1 , 2 , 3 ] ) ) ; expect ( pipe ( R . fromNullable ( null , 'this is bad' ) , R . catchError ( _ => R . Ok ( 'string' ) ) ) ) . toEqual ( R . Ok ( 'string' ) ) ; } ) ; it ( '*' , ( ) => { const { Ok , Error } = R ; let obj = { name : 'Joe' , age : 20 , } ; expect ( pipe ( R . fromNullable ( obj , 'unknown' ) , R . catchError ( err => { return R . Ok ( { name : err , age : 0 , } ) ; } ) , R . map ( obj => obj . name ) ) ) . toEqual ( Ok ( 'Joe' ) ) ; obj = null as unknown as { name : string ; age : number ; } | null ; expect ( pipe ( R . fromNullable ( obj , 'unknown' ) , R . catchError ( err => { return R . Error ( `${ err } value` ) ; } ) , R . map ( obj => obj . name ) ) ) . toEqual ( Error ( 'unknown value' ) ) ; } ) ; } ) ;