Hello!
Does anyone know how to make type inference work when your action returns a Response? The typing breaks and I cannot access anything in the returned data unless I manually type the return value.
Example:
export async function action({ request }: ActionArgs) {
const url = new URL(request.url);
const token = url.searchParams.get('token');
const result = await resetPasswordWithToken(await inputFromForm(request), {
resetToken: token,
});
// If there was an error, we return that JSON back directly.
if (!result.success) {
return {
formError: result.errors.map((error) => error.message).join('\n'),
};
}
// Otherwise we set a flash message and redirect back to the sign in page.
const session = await sessionStorage.getSession(
request.headers.get('Cookie')
);
session.flash('statusMessage', {
type: 'success',
message: result.data.message,
});
return redirect('/sign-in', {
headers: {
'Set-Cookie': await sessionStorage.commitSession(session),
},
});
}
//...
const actionData = useActionData<typeof action>();
The type of actionData is as follows:
const actionData: SerializeObject<UndefinedToOptional<Response>> | SerializeObject<UndefinedToOptional<{ formError: string; }>> | undefined;