import variant , { Outputs , Property , Variant , VariantCreator } from "./variant" ; import { ExtractOfUnion , Func , Identity } from "./util" ; param x param options export function exhaust ( x , options = { key : 'type' , throw : false } ) { const msg = `Switch does not handle all cases. Failed case:${ ( x as any ) [ options . key ] }` ; if ( options . throw ) { throw new Error ( msg ) ; } } param defaults export function fields < T > ( defaults = { } ) { return ( input ) => ( { ... defaults , ... input , } ) as T ; } /**
 * Take a single variable of type T and store as 'payload'
 */ export function payload < T > ( _example ? ) { return ( payload ) => ( { payload } ) ; } export function property < K extends string = 'payload' > ( key ) { return < T > ( ) => ( payload ) => ( { [ key ] : payload } ) as K extends keyof infer KLiteral ? Property < keyof KLiteral & string , T > : never ; } export function data < T > ( x ) { return ( override ? ) => ( { ... x , ... ( override != undefined && override ) } ) ; } param x export function constant < T > ( x ) { return ( ) => x ; } type FlattenToTypeStr < T extends ( string | VariantCreator < string , Func , K > ) , K extends string = 'type' > = T extends VariantCreator < infer R , Func , K > ? R : T extends string ? T : never ; param type export function isType < T extends ( string | VariantCreator < string , Func , K > ) , K extends string = 'type' > ( type ) ; param instance param type param key export function isType < O extends Property < K , string > , T extends ( O [ K ] | VariantCreator < O [ K ] , Func , K > ) , K extends string = 'type' > ( instance , type , key ? ) ; export function isType < O extends Property < K , string > , T extends ( O [ K ] | VariantCreator < O [ K ] , Func , K > ) , K extends string = 'type' > ( instanceOrType , typeOrKey ? , key ? ) { if ( instanceOrType != undefined ) { if ( typeof instanceOrType === 'function' || typeof instanceOrType === 'string' ) { const typeArg = instanceOrType as T ; const typeStr = typeof typeArg === 'string' ? typeArg : ( typeArg as VariantCreator < string , any , K > ) . type ; return < O extends Property < K , string > > ( o ) : o is ExtractOfUnion < O , FlattenToTypeStr < T , K > , K > => isType ( o , typeStr ) ; } else { const instance = instanceOrType as O ; const type = typeOrKey as T ; const typeStr = typeof type === 'string' ? type : ( type as VariantCreator < string , any , K > ) . type ; return instance != undefined && ( instance as Property < K , string > ) [ key ?? 'type' ] === typeStr ; } } else { return false ; } }