Skip to content

Commit

Permalink
adding type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
MikesGlitch committed Nov 18, 2023
1 parent 01aedbe commit 5343a3e
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,21 +684,26 @@ type AutocompleteProps =
| ComponentProps<typeof SingleAutocomplete>
| ComponentProps<typeof MultiAutocomplete>;

function isMultiAutocomplete(
props: AutocompleteProps,
multi?: boolean,
): props is ComponentProps<typeof MultiAutocomplete> {
return multi;
}
function isSingleAutocomplete(
props: AutocompleteProps,
multi?: boolean,
): props is ComponentProps<typeof SingleAutocomplete> {
return !multi;
}

export default function Autocomplete({
multi,
...props
}: AutocompleteProps & { multi?: boolean }) {
if (multi) {
return (
<MultiAutocomplete
{...(props as ComponentProps<typeof MultiAutocomplete>)}
/>
);
} else {
return (
<SingleAutocomplete
{...(props as ComponentProps<typeof SingleAutocomplete>)}
/>
);
if (isMultiAutocomplete(props, multi)) {
return <MultiAutocomplete {...props} />;
} else if (isSingleAutocomplete(props, multi)) {
return <SingleAutocomplete {...props} />;
}
}

0 comments on commit 5343a3e

Please sign in to comment.