Skip to content

Instantly share code, notes, and snippets.

@GabiGrin
Created April 17, 2018 15:13
Show Gist options
  • Save GabiGrin/5e74c631e50f541167695194ac3130a6 to your computer and use it in GitHub Desktop.
Save GabiGrin/5e74c631e50f541167695194ac3130a6 to your computer and use it in GitHub Desktop.
example of how to create a type safe partial change function for value comps
export type Filters = {
channels: string[];
spam: boolean;
assignedId: string;
};
export type FiltersProps = ValueCompProps<Filters>;
export class FiltersView extends React.PureComponent<FiltersProps, {}> {
partialChange = <T extends keyof Filters>(prop: T) => (newVal: Filters[T]) => {
this.props.onChange({...this.props.value, [prop]: newVal});
}
render () {
const {value} = this.props;
return (
<div>
This works:
Span: <Checkbox value={value.spam} onChange={this.partialChange('spam')}/>
this doesn't: it knows channels is not boolean thus not compat with checkbox
{/* Span: <Checkbox value={value.spam} onChange={this.partialChange('channels')}/> */}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment