TypeScript 常见错误与解决方案
1、TS1005、TS2004
ERROR in [at-loader] ./node_modules/@types/d3-dsv/index.d.ts:493:52
TS1005: ',' expected.
ERROR in [at-loader] ./node_modules/@types/d3-dsv/index.d.ts:159:51
TS2304: Cannot find name 'readonly'.
问题原因:
typescript版本太低,需要升级到最新版本
解决方案:
报错时的版本是3.3.3,升级到4.2.3后,报错消失
2、TS2308
ERROR in [ at-loader ] ./node_modules@types/d3/index.d.ts:26:1
TS2308: Module 'd3-array' has already exported a member named 'map' . Consider explicitly re-exporting to resolve the ambiguity.
ERROR in [ at-loader ] ./node_modules@types/d3/index.d.ts:26:1 TS2308: Module 'd3-array' has already exported a member named 'map' . Consider explicitly re-exporting to resolve the ambiguity.
问题原因:
两个文件都导出了同一个名为map的方法,所以ts检测报错
解决方案:
项目的tsconfig.json中,配置编译选项,skipLibCheck设为true。忽略所有的声明文件( *.d.ts
)的类型检查。
3、TS2339
ERROR in [ at-loader ] ./src/wave-artboard/wave-artboard.tsx:233:12
TS2339: Property 'event' does not exist on type 'typeof import("F:/eFlowerProject.BSR-H5/node_modules/@types/d3/index")'.
ERROR in [ at-loader ] ./src/wave-artboard/wave-artboard.tsx:233:12
TS2339: Property 'event' does not exist on type 'typeof import("F:/eFlowerProject.BSR-H5/node_modules/@types/d3/index")'.
问题原因:
项目中使用d3.event从而报错
解决方案:
将d3.event 改成 d3[event]
跳转typescript官方文档请点这:https://www.tslang.cn/docs/handbook/nightly-builds.html