import Context from '../ctx' ; import { TypeOf , Dummy } from '.' ; /**
 * Either
 */ export default class EitherContext < CtxA extends Context , CtxB extends Context , Maybe = Dummy > extends Context < Maybe extends Dummy ? ( TypeOf < CtxA > | TypeOf < CtxB > ) : ( TypeOf < CtxA > | TypeOf < CtxB > | Maybe ) > { public readonly name = 'Either' ; public readonly ctxA : Context ; public readonly ctxB : Context ; constructor ( ctxA , ctxB , optional = false , nullable = false ) { super ( optional , nullable ) ; this . ctxA = ctxA ; this . ctxB = ctxB ; this . push ( v => ctxA . nok ( v ) && ctxB . nok ( v ) ? new Error ( 'not match' ) : true ) ; } public getType ( ) { return super . getType ( `(${ this . ctxA . getType ( ) } | ${ this . ctxB . getType ( ) })` ) ; } public makeOptional ( ) { return new EitherContext ( this . ctxA , this . ctxB , true , false ) ; } public makeNullable ( ) { return new EitherContext ( this . ctxA , this . ctxB , false , true ) ; } public makeOptionalNullable ( ) { return new EitherContext ( this . ctxA , this . ctxB , true , true ) ; } }