I have the following props interface that should technically support 3 different types of functions all over the app. The current issue is with the last function – for some reason, it is not working and showing the following errors.
The function asks to add parentheses, but even if I do that, I still get an error.
// In Parent component
const handleSwitch = () => {
if (!checked) {
setLocalGender('male');
} else {
setLocalGender('female');
}
setChecked(!checked);
};
<ReusableComponent disabled={false} name='preference' bool={checked} onChangeFn={handleSwitch} />
// Reusable component
interface Props {
disabled: boolean;
name: string;
bool: boolean;
onChangeFn: (e: React.ChangeEvent<HTMLInputElement>) => Promise<void> | (() => Promise<void>) | (() => void)
}
<Switch name={name} disabled={disabled} checked={bool} onChange={onChangeFn} />
Type ‘() => void’ is not assignable to type ‘(e: ChangeEvent) => Promise | (() => Promise) | (() => void)’.
Type ‘void’ is not assignable to type ‘Promise | (() => Promise) | (() => void)’.ts(2322)The expected type comes from property ‘onChangeFn’ which is declared here on type ‘IntrinsicAttributes & Props’
(property) Props.onChangeFn: (e: React.ChangeEvent) => Promise | (() => Promise) | (() => void)
2
Answers
Your function type definition is:
So it can either return
Promise<void>
or two different types of function.However, you are trying to pass in a function that just returns
void
. This is distinct from returningPromise<void>
. If it should be allowed, then you need to addvoid
to your union:I dont find any issues with your typescript declaration this is a working code that i reproduce :
you can check it in sandbox :
https://codesandbox.io/s/react-hook-form-mui-switch-forked-rxks2z?file=/src/App.tsx:0-1513