Oleg
Oleg

Reputation: 1168

Flow js doesn`t apply generic

I have a function (react hook):

function useHandles<TPreparedValues, TValues>({
    onReject,
    makeData,
}: ArgsType<TPreparedValues, TValues>) {
    const handleLeavePage =  (formData: TValues) => onReject(makeData(formData))

    return {
      handleLeavePage
    }
}

Where ArgsType is:

type ArgsType<TPreparedValues, TValues> = {
    onReject: (TPreparedValues) => Promise<void>,
    makeData: TValues => TPreparedValues
};

Then I use this hook:

const {
    handleLeavePage,
} = useDocumentHandles<DocumentEditInputType, ValuesType>({
    onReject,
    makeData,
})

As you see I define that TValues = ValuesType. Of course, flowjs should guess it from onReject typing.

But when I use handleLeavePage, I get an error: TValues [1] is incompatible with ValuesType [2] in the first argument of property onLeavePage.

I think flowjs doesn't understand that TValue is generic in handleLeavePage .

How to fix it?

Upvotes: 0

Views: 43

Answers (1)

Alex Savin
Alex Savin

Reputation: 528

Try this

Instead of declaring type of the handleLeavePage argument, type the output of the useHandles function.

Upvotes: 1

Related Questions