传递性npm依赖immer引发TS6133错误的解决咨询
解决TS6133:传递依赖immer中未使用变量的类型检查错误
问题描述
项目安装了jdm-editor(其依赖包含immer),执行tsc --noEmit时触发TS6133错误:
npm run type-check > my-ui@1.0.0 type-check > tsc --noEmit node_modules/immer/src/utils/common.ts:207:33 - error TS6133: 'key' is declared but its value is never read. 207 Object.entries(obj).forEach(([key, value]) => freeze(value, true)) ~~~ Found 1 error in node_modules/immer/src/utils/common.ts:207
已尝试调整skipLibCheck配置、单独安装immer、固定immer版本,均未解决问题。当前tsconfig.json配置如下:
{ "compilerOptions": { "allowJs": true, "allowSyntheticDefaultImports": true, "alwaysStrict": true, "declaration": true, "emitDecoratorMetadata": true, "esModuleInterop": true, "experimentalDecorators": true, "forceConsistentCasingInFileNames": true, "importHelpers": true, "incremental": true, "isolatedModules": true, "jsx": "react-jsx", "lib": ["dom", "dom.iterable", "esnext"], "module": "esnext", "noFallthroughCasesInSwitch": true, "noImplicitAny": true, "noUnusedLocals": true, "noUnusedParameters": true, "moduleResolution": "node", "noEmit": true, "removeComments": true, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, "strict": true, "strictBindCallApply": false, "strictNullChecks": true, "target": "esnext", "typeRoots": ["./src/@types", "./node_modules/@types"] }, "exclude": ["./node_modules", "../typings"], "include": ["./src", "./vite.config.ts", "./jest.config.ts"] }
解决方案
方案1:精确排除node_modules下的所有源码文件
当前exclude配置可能未完全覆盖node_modules下的.ts源码文件,修改exclude为递归排除路径:
"exclude": ["node_modules/**/*", "../typings"]
node_modules/**/*会彻底排除node_modules下所有文件,避免TypeScript扫描到immer的源码文件。
方案2:仅在业务代码中启用未使用变量检查
全局开启noUnusedLocals和noUnusedParameters会影响所有被扫描到的文件(包括传递依赖的源码),可以拆分tsconfig配置,仅在业务代码中启用严格检查:
- 根目录创建
tsconfig.base.json,移除全局的未使用变量检查规则:
{ "compilerOptions": { "allowJs": true, "allowSyntheticDefaultImports": true, "alwaysStrict": true, "declaration": true, "emitDecoratorMetadata": true, "esModuleInterop": true, "experimentalDecorators": true, "forceConsistentCasingInFileNames": true, "importHelpers": true, "incremental": true, "isolatedModules": true, "jsx": "react-jsx", "lib": ["dom", "dom.iterable", "esnext"], "module": "esnext", "noFallthroughCasesInSwitch": true, "noImplicitAny": true, "moduleResolution": "node", "noEmit": true, "removeComments": true, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, "strict": true, "strictBindCallApply": false, "strictNullChecks": true, "target": "esnext", "typeRoots": ["./src/@types", "./node_modules/@types"] }, "exclude": ["node_modules/**/*", "../typings"] }
- 创建
tsconfig.src.json继承基础配置,仅在业务代码中启用未使用变量检查:
{ "extends": "./tsconfig.base.json", "compilerOptions": { "noUnusedLocals": true, "noUnusedParameters": true }, "include": ["./src", "./vite.config.ts", "./jest.config.ts"] }
- 修改package.json中的type-check命令:
"scripts": { "type-check": "tsc --noEmit -p tsconfig.src.json" }
方案3:用ESLint规则替代全局TypeScript检查
如果项目使用ESLint,可以关闭TypeScript全局的未使用变量规则,改用@typescript-eslint/no-unused-vars(默认仅检查业务代码):
- 在tsconfig.json中关闭全局规则:
"noUnusedLocals": false, "noUnusedParameters": false
- 在ESLint配置中启用对应规则:
{ "rules": { "@typescript-eslint/no-unused-vars": "error" } }
内容的提问来源于stack exchange,提问作者likava
相关产品推荐
相关产品推荐

