Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript error when passing null to selectItem #1459

Closed
WinSanPang opened this issue Jan 6, 2023 · 1 comment
Closed

Typescript error when passing null to selectItem #1459

WinSanPang opened this issue Jan 6, 2023 · 1 comment

Comments

@WinSanPang
Copy link

  • downshift version: 6.1.7
  • node version: 14.15.4
  • npm (or yarn) version: 1.22.19

Relevant code or config

type Item = {
  id: string;
  name: string;
  information: string;
  warning: boolean;
}
const [itemList, setItemList] = React.useState<Item|null>(null);
const {
    isOpen,
    getMenuProps,
    getInputProps,
    getComboboxProps,
    getItemProps,
    closeMenu,
    selectItem,
    selectedItem
  } = useCombobox({
    items: itemList,
    itemToString: () => '',
    onSelectedItemChange: ({ selectedItem }) => {
      if (selectedItem) {
        onSelection(selectedItem)
      }
    }

  useEffect(() => {
    if (clearSelectItem) {
      selectItem(null)
    }
  }, [clearSelectItem, selectItem, selectedItem])

What you did:

  • Following the example in the docs, I'm trying to set selectItem to null on an action triggered by the user (click an X button for the item previously selected + rendered on the page to remove it)

What happened:

  • The action works as expected, but Typescript errors, stating we can't pass null to selectItem: Argument of type 'null' is not assignable to parameter of type 'Item'.ts(2345)

Problem description:

  • Tracing the previous thread issue 1088, it seems like we allow selectedItem to be null and various arguments within onChange, onSelect, etc. but selectItem is strictly typed against Item

Suggested solution:

  • Update selectItem to also accept null as an argument
@WinSanPang
Copy link
Author

Lol nevermind, realise this has been fixed in later version 7. Ignore me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant