downshift
downshift copied to clipboard
Typescript: Have non optional type for state property in its onChange handler
Relevant code or config
Avoid doing this:
onSelectedItemChange: ({selectedItem}) => {
selectedItem && addSelectedItem(selectedItem)
},
Problem description:
Since it's the handler for onSelectedItemChange
the types should guarantee that selectedItem
is defined.
Suggested solution:
The types use the same interface as onStateChange
where all state properties are optional. Each onChange handler should extend this default interface and add its own property as non optional.
export interface UseSelectStateChange<Item>
extends Partial<UseSelectState<Item>> {
type: UseSelectStateChangeTypes
}
// this should be added for all on change handlers.
export interface UseSelectSelectedItemChange<Item>
extends UseSelectStateChange<Item> {
selectedItem: Item
}
// and then
export interface UseSelectProps<Item> {
onSelectedItemChange?: (changes: UseSelectSelectedItemChange<Item>) => void,
// the rest
}