I'm trying to set up a type for a custom input component that will inherit properties based on the type of element to be displayed. Essentially the logic is what the title states
type A AND EITHER B or C or D or E
meaning A is always applied, then either B or C or D or E follow. Here's what I have so far.
interface GenericInputElementProps {
id: string;
hidden: boolean;
label: string;
onInput: (
id: string,
value: string | number,
isValid: boolean
) => {
type: string;
value: string | number;
inputId: string;
isValid: boolean;
};
validators?: { type: string; configVal?: number }[];
initialValue?: {
initialValue: string;
initialValid: boolean;
};
}
type InputElementProps =
| (
| GenericInputElementProps
| {
element: 'input';
type: string;
placeholder: string;
errorText: string;
}
)
| {
element: 'textarea';
rows: number;
placeholder: string;
errorText: string;
}
| { element: 'number'; type: 'number' }
| { element: 'checkbox'; type: 'checkbox' }
| { element: 'select'; sizes: ISizes[] };
but this doesn't work because typescript thinks I'm using GenericInputElementProps and element='input' but doesn't consider the other options. I'm sure there is a way around this but I cannot find out what pattern I can use.