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

NextJS+TypeScript构建报错:OmitWithTag类型不满足索引签名约束

Next.js + TypeScript 构建错误求助

构建错误信息

执行pnpm run build时触发TypeScript类型检查失败,错误详情如下:

✓ Compiled successfullyLinting and checking validity of types  ..Failed to compile. .next/types/app/components/button/addNew/page.ts:

Type error: Type 'OmitWithTag<ButtonProps, keyof PageProps, "default">' does not satisfy the constraint '{ [x: string]: never; }'.Property 'onClick' is incompatible with index signature.Type 'MouseEventHandler<HTMLButtonElement>' is not assignable to type 'never'.

// Check the prop type of the entry function
28 | checkFields<Diff<PageProps, FirstArg<TEntry['default']>, 'default'>>()|             ^29 |30 | // Check the arguments and return type of the generateMetadata function31 | if ('generateMetadata' in entry) { ELIFECYCLE  Command failed with exit code 1.

错误代码位于.next/src/app/components/button/addNew/page.ts的第28行:

// Check the prop type of the entry function 
checkFields<Diff<PageProps, FirstArg<TEntry['default']>, 'default'>>()

组件代码(可正常运行,但构建报错)

路径:src/app/components/button/addNew/page.tsx

interface ButtonProps {
  onClick: React.MouseEventHandler<HTMLButtonElement>;
}

export default function NewTaskButton({ onClick }: ButtonProps) {
  return (
    <div>
      <button className="btn btn-accent btn-circle font-extrabold text-xl w-15 h-15" onClick={onClick}>+</button>
    </div>
  );
}

tsconfig.json 配置

{   
  "compilerOptions": {     
    "lib": [       
      "dom",       
      "dom.iterable",       
      "esnext"     
    ],     
    "allowJs": true,     
    "skipLibCheck": true,     
    "strict": true,     
    "noEmit": true,     
    "esModuleInterop": true,     
    "module": "esnext",     
    "moduleResolution": "bundler",     
    "resolveJsonModule": true,     
    "isolatedModules": true,     
    "jsx": "preserve",     
    "incremental": true,     
    "plugins": [       
      {         
        "name": "next"       
      }     
    ],     
    "paths": {       
      "@/*": [         
        "./src/*"       
      ]     
    }   
  },   
  "exclude": [     
    "node_modules"   
  ],   
  "include": [     
    "next-env.d.ts",     
    "**/*.ts",     
    "**/*.tsx",     
    ".next/types/**/*.ts"   
  ] 
}

已尝试的无效解决方法

  • 清除TypeScript缓存
  • 重装node_modules
  • 删除.next文件夹
  • 将ButtonProps接口移至外部文件导入
  • 用any类型替代ButtonProps

求可行的排查与解决思路。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 10:12:05