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

使用Expo时tsc持续追踪node_modules问题求助

问题

使用Expo开发时,tsconfig.json继承了expo/tsconfig.base(该配置已明确排除node_modules),但执行原生tsc命令时,仍会扫描node_modules/react-native-gifted-charts并报大量错误。

我的tsconfig.json配置

{
    "compilerOptions": {
        "paths": {
            "@/*": [
                "./src/*"
            ],
            "@assets/*": [
                "./assets/*"
            ]
        },
        "strict": true,
        "strictFunctionTypes": false,
        "noFallthroughCasesInSwitch": true
    },
    "include": [
        "src",
        ".expo/types/**/*.ts",
        "expo-env.d.ts"
    ],
    "extends": "expo/tsconfig.base"
}

继承的expo/tsconfig.base配置

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Expo",

  "compilerOptions": {
    "allowJs": true,
    "esModuleInterop": true,
    "jsx": "react-native",
    "lib": ["DOM", "ESNext"],
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "target": "ESNext"
  },

  "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
}

tsc命令执行后的错误示例

$ yarn tsc

...

Errors  Files
     1  node_modules/react-native-gifted-charts/src/BarChart/Animated2DWithGradient.tsx:110
     8  node_modules/react-native-gifted-charts/src/BarChart/index.tsx:19
     3  node_modules/react-native-gifted-charts/src/BarChart/RenderBars.tsx:219
     1  node_modules/react-native-gifted-charts/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/index.tsx:8
     3  node_modules/react-native-gifted-charts/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/renderDataPoints.tsx:7
     1  node_modules/react-native-gifted-charts/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/renderSpecificDataPoints.tsx:5
     1  node_modules/react-native-gifted-charts/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/renderSpecificVerticalLines.tsx:4
     2  node_modules/react-native-gifted-charts/src/Components/BarAndLineChartsWrapper/renderVerticalLines.tsx:6
     1  node_modules/react-native-gifted-charts/src/Components/BarSpecificComponents/barBackgroundPattern.tsx:4
     1  node_modules/react-native-gifted-charts/src/Components/BarSpecificComponents/cap.tsx:5
     1  node_modules/react-native-gifted-charts/src/Components/common/Pointer.tsx:4
     1  node_modules/react-native-gifted-charts/src/Components/common/StripAndLabel.tsx:6
    37  node_modules/react-native-gifted-charts/src/LineChart/index.tsx:411
    16  node_modules/react-native-gifted-charts/src/LineChart/LineChartBicolor.tsx:193
     2  node_modules/react-native-gifted-charts/src/PieChart/index.tsx:32
error Command failed with exit code 2.

注:tsc为原生命令,未在package.json中做任何自定义配置。


原因分析与解决方法

核心原因

  1. skipLibCheck不覆盖源码检查:expo/tsconfig.base里的skipLibCheck: true只跳过.d.ts类型声明文件的检查,但react-native-gifted-charts是直接发布TS/TSX源码,没有编译成JS+声明文件的形式,TS会把这些源码当作项目文件扫描。
  2. 导入路径触发强制检查:如果代码里直接导入了该包的内部模块(比如import X from 'react-native-gifted-charts/src/xxx'),TS会强制检查这些导入的文件,不受exclude规则限制。

解决步骤

  • 方案一:强制排除该包所有文件
    在自己的tsconfig.json里单独把react-native-gifted-charts加入exclude数组,注意要保留继承配置里的原有排除项:

    {
      // ... 其他配置
      "exclude": [
        "node_modules",
        "babel.config.js",
        "metro.config.js",
        "jest.config.js",
        "node_modules/react-native-gifted-charts/**/*"
      ]
    }
    
  • 方案二:不要导入包的内部源码
    检查代码,只导入包的顶层导出(比如import { BarChart } from 'react-native-gifted-charts'),避免直接引用src目录下的文件,这样TS只会检查包的类型声明(如果存在),不会扫描源码。

  • 方案三:自定义tsc命令范围
    在package.json里添加脚本,明确指定要检查的文件,避免TS扫描无关内容:

    {
      "scripts": {
        "tsc": "tsc --project tsconfig.json --noEmit src/**/* .expo/types/**/*.ts expo-env.d.ts"
      }
    }
    

    之后执行yarn tsc就只会检查你指定的文件,不会碰node_modules里的内容。

  • 方案四:升级或替换依赖
    查看react-native-gifted-charts的最新版本,是否修复了TS类型问题或发布了正确的类型声明;如果没有,可考虑替换为类型支持更完善的图表库。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 19:23:29