Skip to content

Typescript: Have non optional type for state property in its onChange handler #1225

Description

@silviuaavram

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions