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

Type Error: 属性'comments'不存在于'IntrinsicAttributes'类型求助

问题分析与修复方案

错误本质

出现的TypeScript错误是因为你在AppComponent中向<Component>传递的comments、setTab等属性,未被定义在页面组件的Props类型中,TypeScript校验时判定这些属性不属于React元素的基础属性(IntrinsicAttributes)。

两台服务器构建结果不同,核心原因是依赖版本或TypeScript配置不一致:server02可能使用了更低版本的TypeScript/Next.js,或配置更宽松,跳过了严格的类型检查。

修复步骤

1. 补全页面组件的Props类型定义

修改NextPageWithLayout类型,把所有传递给页面的属性纳入类型声明:

import { Dispatch, SetStateAction } from "react";
import { AxiosResponse } from "axios";
import { EmotionCache } from "@emotion/react";

type NextPageWithLayout = NextPage & {
  getLayout?: (page: ReactElement) => ReactNode;
  // 新增传递给页面的属性类型
  comments?: Promise<any> | null | void;
  setTab: Dispatch<SetStateAction<string>>;
  tab: string;
  likes: AxiosResponse | number;
  path: string;
  emotionCache?: EmotionCache;
};

2. 统一两台服务器的依赖版本

执行以下命令锁定依赖,确保两台服务器环境一致:

# 删除现有依赖和锁文件
rm -rf node_modules package-lock.json
# 重新安装依赖
npm install

将生成的package-lock.json同步到两台服务器,避免依赖版本差异。

3. 临时方案:降低TypeScript校验严格度(不推荐)

若需快速验证,可临时修改tsconfig.json放宽检查,但不建议长期使用:

{
  "compilerOptions": {
    "strict": false,
    "noImplicitAny": false
  }
}

冗余代码清理

你的AppPropsWithLayout类型中,comments、setTab、tab、likes这些属性是AppComponent内部创建的状态,并非外部传入的Props,属于冗余定义,建议删除:

type AppPropsWithLayout = AppProps & {
  Component: NextPageWithLayout;
  path: string;
  emotionCache?: EmotionCache;
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 09:10:44