You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Inertia.js+TypeScript报错:表单post方法参数类型不兼容

问题原因与解决方案

这是操作有误,并非TypeScript本身的问题。核心原因是你混淆了Inertia两种提交方式的参数规范:

  • useForm返回的表单实例提供的submit方法支持onFinish选项,但这个选项不属于Inertia全局post方法的VisitOptions类型定义,直接给post传onFinish会触发类型校验错误。
  • 编辑器提示“需添加所有成员”,是因为TypeScript检测到你传入了VisitOptions中不存在的属性,无法完成类型匹配。

正确用法示例

使用useForm创建的表单,应该调用表单实例的submit方法提交,并传入onFinish:

import { useForm } from '@inertiajs/react';

// 初始化表单
const form = useForm({
  name: '',
  email: '',
  password: '',
  password_confirmation: '',
});

// 提交逻辑
const submitForm = () => {
  form.submit('post', route('register'), {
    onFinish: () => {
      // 表单提交完成后的处理逻辑(无论请求成功/失败都会触发)
      form.reset();
    },
    // 可选:仅在请求成功时触发的回调
    onSuccess: () => {
      console.log('注册成功');
    }
  });
};

若直接使用Inertia.post

此时不能用onFinish,需改用VisitOptions中定义的onSuccess(请求成功)或onError(请求失败)回调:

import { Inertia } from '@inertiajs/react';
import { useForm } from '@inertiajs/react';

const form = useForm({
  name: '',
  email: '',
  password: '',
  password_confirmation: '',
});

const submitForm = () => {
  Inertia.post(route('register'), form.data(), {
    onSuccess: () => {
      console.log('请求成功');
      form.reset();
    },
    onError: (errors) => {
      console.log('请求失败', errors);
    }
  });
};

内容的提问来源于stack exchange,提问作者Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 19:00:06