I'm quite new to Typescript and I'm trying to have a conditional extends based on a function argument value. The context is to define a widget for object selection that can be a select, a list of radio (a radio group) or a list of checkbox (a checkbox group). Each of this different widget accept different props, name: SelectProps, RadioGroupProps and CheckboxGroupProps. The widget selection is based on two parameters: expand (means we want a list of checkbox or radio) and multiple (means we want to select multiple elements or not).
function SelectionWidget(props: SelectionWidgetProps) {}
Here, I want that:
if props matches {expand: true, multiple: false} then SelectionWidgetProps must extends RadioGroupProps.
if props matches {expand: true, multiple: true} then SelectionWidgetProps must extends CheckboxGroupProps.
otherwise, SelectionWidgetProps must extends SelectProps.
I tried the following:
type SelectTypeExtend<T> =
T extends { expand: false } ? SelectProps :
T extends { expand: true; multiple: false } ? RadioGroupProps :
T extends { expand: true; multiple: true; } ? CheckboxGroupProps :
never;
interface SelectTypeProps extends SelectTypeExtends {
multiple?: boolean;
expand?: boolean;
}
But it doesn't work as SelectTypeExtends expects an argument.
Is it possible to achieve this with Typescript?
Thanks!