构建React应用时@types/leaflet类型定义文件出现多个TS语法错误的求助
构建React应用时@types/leaflet类型定义文件出现多个TS语法错误的求助
大家好,我最近遇到了一个非常棘手的问题:本地运行React应用时完全正常,没有任何类型错误或者其他异常,但执行构建命令时,@types/leaflet的类型定义文件突然爆出一堆TypeScript语法错误,试了好几种办法都没解决,想请教一下有没有前辈遇到过类似情况?
问题详情
当我执行npm run build时,触发的compile脚本(yarn configDeps && tsc -b)会在../../node_modules/@types/leaflet/index.d.ts文件中抛出13个语法错误,部分错误示例如下:
../../node_modules/@types/leaflet/index.d.ts:2504:51 - error TS1144: '{' or ';' expected. 2504 ): { new(...args: any[]): T } & typeof Control<Options>; ~ ../../node_modules/@types/leaflet/index.d.ts:2504:60 - error TS1109: Expression expected. 2504 ): { new(...args: any[]): T } & typeof Control<Options>; ~ ../../node_modules/@types/leaflet/index.d.ts:2505:25 - error TS1109: Expression expected. 2505 constructor(options?: Options); ~
这些错误直接导致compile脚本执行失败,构建流程终止。
我的环境依赖版本
- React:
"16.11.0" - TypeScript:
"4.1.3" - 地图相关依赖:
react-leaflet:"~3.1.0"leaflet:"~1.7.1"@types/leaflet:"1.5.24"
已尝试的解决方案
我已经试过以下几种常见的排查方法,但都没有效果:
- 在
tsconfig.json中启用skipLibCheck: true,忽略库文件的类型检查 - 在涉及leaflet的组件文件中添加
@ts-ignore或@ts-nocheck注释 - 多次升级/降级
react-leaflet、leaflet和@types/leaflet的版本,尝试过不同的版本组合 - 清理
node_modules和lock文件后重新安装依赖
我的package.json脚本配置(web包)
"scripts": { "configDeps": "copyfiles -a ../../node_modules/@pdftron/webviewer/public/. ./public/lib", "compile": "yarn configDeps && tsc -b", "prestart": "npm run compile", "start": "concurrently \"npm run compile -- -w --incremental\" \"HTTPS=true npm run _start\"", "windows_start": "concurrently \"npm run compile -- -w --incremental\" \"npm run _start\"", "_start": "set NODE_OPTIONS=--openssl-legacy-provider && react-app-rewired start", "prebuild": "npm run compile", "build": "set NODE_OPTIONS=--openssl-legacy-provider && react-app-rewired build", "pretest": "npm run compile", "test": "react-app-rewired test", "eject": "react-app-rewired eject", "lint": "eslint 'src/*.{js,ts,tsx}' --fix" }, "browserslist": [ ">0.2%", "not dead", "not ie <= 11", "not op_mini all" ]
有没有人能帮我分析一下问题的根源?或者提供一些其他可以尝试的解决方案?
内容来源于stack exchange
相关产品推荐
相关产品推荐

