运行amplify serve时@aws-amplify/core报TypeScript错误的解决咨询
问题描述
本地运行React.js/TypeScript应用的amplify serve命令时,出现Amplify库相关的TypeScript错误,相关配置及错误信息如下:
依赖版本(package.json片段)
{ "dependencies": { "@aws-amplify/core": "^5.1.12", "@aws-amplify/ui-react": "^4.6.0", "@aws-amplify/ui-react-storage": "^1.1.0", "aws-amplify": "^5.1.3", "typescript": "^3.8.3" } }
tsconfig.json配置
{ "compilerOptions": { "target": "es5", "lib": ["es6", "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "strict": true, "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, "plugins": [{ "name": "typescript-plugin-css-modules" }], "jsx": "react" }, "include": ["src"], "exclude": ["node_modules"] }
出现的TypeScript错误
错误1
ERROR in node_modules/@aws-amplify/core/src/Hub.ts:82:34 TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'. 80 | this.patterns = [...this.patterns.filter(x => x !== pattern)]; 81 | } else { > 82 | const holder = this.listeners[channel]; | ^^^^^^^ 83 | if (!holder) { 84 | logger.warn(`No listeners for ${channel}`); 85 | return;
错误2
ERROR in node_modules/@aws-amplify/core/src/Logger/ConsoleLogger.ts:33:10 TS2564: Property '_config' has no initializer and is not definitely assigned in the constructor. 31 | level: LOG_TYPE | string; 32 | private _pluggables: LoggingProvider[]; > 33 | private _config: object; | ^^^^^^^ 34 | 35 | /** 36 | * @constructor
错误3
ERROR in node_modules/@aws-amplify/core/src/Logger/ConsoleLogger.ts:80:4 TS2322: Type 'null' is not assignable to type 'string'. 78 | let logger_level_name = this.level; 79 | if (ConsoleLogger.LOG_LEVEL) { > 80 | logger_level_name = ConsoleLogger.LOG_LEVEL; | ^^^^^^^^^^^^^^^^^ 81 | } 82 | if (typeof (<any>window) !== 'undefined' && (<any>window).LOG_LEVEL) { 83 | logger_level_name = (<any>window).LOG_LEVEL;
原因分析
- TypeScript版本不兼容:使用的TypeScript 3.8.3版本过旧,Amplify v5+的类型定义基于更高版本TS(至少4.x)编写,旧版本无法正确解析新类型规则。
- Amplify库类型缺陷:部分Amplify源码存在类型不严谨问题,比如未明确索引类型、属性未初始化、null赋值给非null变量,在TS严格模式下触发错误。
- TS严格模式冲突:
tsconfig.json开启了strict: true(包含noImplicitAny、strictNullChecks等规则),放大了Amplify类型定义的问题。
解决方案
1. 升级TypeScript版本(推荐)
这是最根本的解决办法,升级到与Amplify v5兼容的TS版本(推荐4.9+):
npm install typescript@^4.9.0 --save-dev # 或用yarn yarn add typescript@^4.9.0 --dev
2. 确保skipLibCheck生效
虽然配置里已经设了skipLibCheck: true,但可简化exclude配置确保其生效:
"exclude": ["node_modules"]
该选项会让TS跳过第三方库的类型检查,直接忽略库内部的类型错误。
3. 升级Amplify依赖到最新稳定版
Amplify团队会持续修复类型问题,升级到v5最新兼容版本:
npm install aws-amplify@^5.3.0 @aws-amplify/core@^5.4.0 @aws-amplify/ui-react@^4.8.0 @aws-amplify/ui-react-storage@^1.3.0 --save # 或用yarn yarn upgrade aws-amplify@^5.3.0 @aws-amplify/core@^5.4.0 @aws-amplify/ui-react@^4.8.0 @aws-amplify/ui-react-storage@^1.3.0
4. 临时忽略特定错误(不推荐长期使用)
若暂时无法升级,可临时关闭部分TS严格规则:
"compilerOptions": { // 其他配置保持不变 "noImplicitAny": false, "strictPropertyInitialization": false, "strictNullChecks": false }
注意:关闭严格规则会降低TS类型检查能力,可能引入潜在问题,仅作临时方案。
内容的提问来源于stack exchange,提问作者Raystorm
相关产品推荐
相关产品推荐

