I am using React and TypeScript and want to pass a useState
function into another function named createReferral
.
const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
createReferral: (
referralData: ReferralFormData,
referralMasterId: string | null,
baseRoute: string,
callbackFunction: () => React.Dispatch<React.SetStateAction<boolean>>,
propertyDetails?: PropertyDetails,
successFunction?: () => void,
failureFunction?: () => void
) => { message: { message: string; statusType: string } } | void
When I call my createReferral
function I get the following error:
Argument of type '() => void' is not assignable to parameter of type 'string'.
Here is my function call:
createReferral(
configuredReferralData,
referralMasterId,
baseRoute || '',
() => setIsSubmitting(false),
propertyData,
(successFunction = successFunction),
(failureFunction = failureFunction)
)
I am really stuck, not sure why the error is saying I am assigning something to type ‘string’
Alright, I flipped the props around. Now the error message reads:
Type 'void' is not assignable to type 'Dispatch<SetStateAction<boolean>>'
2
Answers
Not sure what you’re trying to achieve by having so many arguments passed into a function but I noticed that you’re not typing the useState setter function correctly and that might be the reason for the error you’re experiencing. The correct type should be:
You can’t be really stuck. If you want to call your function this way:
Then your function signature needs be set up accordingly to that:
Thatswhy typescript tells you to not assign a () => void into the parameter of a string (in your case baseRouter)..