import {Component} from 'react' export function getDefault(targetValue: T, defaultValue: T): T { return typeof targetValue === 'undefined' ? defaultValue : targetValue } function genericSetState(comp: Component>, state: State) { comp.setState(state) } function genericGetState(comp: Component>, prop: K | K[]) { if (typeof prop === 'string') { return comp.state[prop as K] } else { return (prop as K[]).map( propName => comp.state[propName] ) } } export function createStateOperator(comp: Component>): [ (propName: K | K[]) => State[K] | Array, (state: Partial) => void ] { return [ // @ts-ignore for generic get state (propName: K | K[]) => { return genericGetState(comp, propName) }, (state: Partial) => genericSetState(comp, state), ] } export type TypeGetState = ReturnType[0] export type TypeSetState = ReturnType[1]