I'm trying to build an interface, for example, Dropdown props interface for React component, and I'm trying to make params optional only if T generic is string. My research found zero answers for my question.
as you can see here, I'm trying to make getTemplate optional only if T extends string type, but if it something else I will have to write it.
Unfortunately it doesn't work. param with undefined type is not optional parameter, it is parameter with undefined.
Any suggestions? Thanks ahead.
here an example of something I tried but doesn't work:
type Props<T extends string | Omit<any, 'string'>> = (T extends string
? {}
: {
getTemplate: (item: T) => ReactNode;
getDisplayTemplate(item: T): ReactNode;
getItemIdentifier(item: T): string | number;
}) & {
items: Array<T>;
disabled?: boolean;
className?: string;
placeholder?: string;
selectedItems: Array<T>;
isMultiSelect?: boolean;
onChange(items: Array<T>): void;
};
export const Dropdown = <T,>({
disabled,
placeholder,
selectedItems = [],
items,
onChange,
getDisplayTemplate,
getItemIdentifier,
getTemplate,
isMultiSelect,
className,
}: Props) => {
