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

如何配置让TS2353类错误触发构建或Lint流程失败?

问题

我的代码行const { data: items } = useFindItems({ owner: user.id });在IntelliJ中能正确提示TS2353错误:TS2353: Object literal may only specify known properties, and owner does not exist in type FindItemsQuery,即owner字段不属于该选项对象的类型。但运行eslint或expo build时该错误未被捕获,错误代码已流入正式构建。我尝试调整ESLint规则和tsconfig.json配置但未解决问题,请问如何配置才能让此类错误触发构建或Lint流程失败?

配置文件

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react-native",
    "lib": [
      "esnext"
    ],
    "types": [
      "react-native",
      "jest",
      "node"
    ],
    "moduleResolution": "node",
    "noEmit": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUncheckedIndexedAccess": true,
    "target": "esnext",
    "noImplicitReturns": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "allowUnreachableCode": false,
    "paths": {
      "@firebase/auth": [
        "./node_modules/@firebase/auth/dist/index.rn.d.ts"
      ]
    }
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ],
  "extends": "expo/tsconfig.base"
}

expo/tsconfig.json

{
  "$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"]
}
解决方案

1. 让TypeScript检查强制拦截构建

Expo默认不会自动执行TypeScript类型检查,需在package.json中添加前置脚本:

{
  "scripts": {
    "type-check": "tsc --noEmit",
    "prebuild": "npm run type-check"
  }
}

此后每次执行expo build前会先运行TypeScript类型检查,一旦发现TS2353这类错误就会终止构建流程。

2. 配置ESLint捕获类型相关错误

确保已安装@typescript-eslint/eslint-plugin,然后在ESLint配置文件(如.eslintrc.js)中启用类型检查规则:

module.exports = {
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking'
  ],
  parserOptions: {
    project: ['./tsconfig.json']
  },
  rules: {
    '@typescript-eslint/no-explicit-any': 'error',
    '@typescript-eslint/no-unsafe-assignment': 'error',
    '@typescript-eslint/no-unsafe-member-access': 'error',
    '@typescript-eslint/ban-ts-comment': 'error',
    '@typescript-eslint/no-unused-vars': 'error'
  }
};

核心是启用plugin:@typescript-eslint/recommended-requiring-type-checking扩展,它会开启依赖类型信息的规则,包括捕获对象字面量未知属性的错误。

3. 确认tsconfig的严格模式生效

虽然你的tsconfig.json设置了strict: true,但由于继承了expo/tsconfig.base,需确保基础配置未覆盖严格模式。可将strict相关配置移到compilerOptions的最上方,或移除继承后手动配置必要项,保证类型检查的严格性。

4. 验证配置效果

执行以下命令测试:

  • npm run type-check:应捕获到TS2353错误
  • eslint . --ext .ts,.tsx:应触发错误并终止Lint流程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 17:03:22