gulp服务显示[tsc]打字稿版本:3.3.4000,而package.json具有“打字稿”:“ ^ 4.0.3”

问题描述

我已经在我的react应用程序中安装了jest和testing-creat-library,但是当我进行gulp构建时,出现以下错误

[14:11:27] Error - [tsc] node_modules/@testing-library/dom/node_modules/pretty-format/build/index.d.ts(7,13): error TS1005: '=' expected.
[14:11:27] Error - [tsc] node_modules/@testing-library/dom/node_modules/pretty-format/build/index.d.ts(7,18): error TS1005: ';' expected.
[14:11:27] Error - [tsc] node_modules/@testing-library/dom/node_modules/pretty-format/build/index.d.ts(7,31): error TS1005: ';' expected.
[14:11:27] Error - [tsc] node_modules/@testing-library/dom/node_modules/pretty-format/build/index.d.ts(7,36): error TS1005: ';' expected.
[14:11:27] Error - 'tsc' sub task errored after 3.57 s

github issue page之后,我将打字稿从3.8.3升级到了4.0.3。它仍然不能解决我的问题。

但是当我运行gulp build时,会看到以下内容[tsc] typescript version: 3.3.4000 为什么在package.json中我指定了更高版本的同时仍然引用旧版本?

如何解决以上错误


package.json

 "dependencies": {
    "@microsoft/sp-core-library": "1.11.0","@microsoft/sp-lodash-subset": "1.11.0","@microsoft/sp-office-ui-fabric-core": "1.11.0","@microsoft/sp-property-pane": "1.11.0","@microsoft/sp-webpart-base": "1.11.0","@testing-library/jest-dom": "^5.11.4","@testing-library/react": "^11.0.4","@types/jest": "^26.0.14","@types/node": "^14.11.8","identity-obj-proxy": "^3.0.0","jest": "^26.5.2","jest-junit": "^12.0.0","office-ui-fabric-react": "6.214.0","react": "16.8.5","react-dom": "16.8.5","react-test-renderer": "^16.13.1","ts-jest": "^26.4.1","typescript": "^4.0.3"
  },"devDependencies": {
    "@microsoft/rush-stack-compiler-3.3": "0.3.5","@microsoft/sp-build-web": "1.11.0","@microsoft/sp-module-interfaces": "1.11.0","@microsoft/sp-tslint-rules": "1.11.0","@microsoft/sp-webpart-workbench": "1.11.0","@types/chai": "3.4.34","@types/es6-promise": "0.0.33","@types/mocha": "2.2.38","@types/react": "^16.8.8","@types/react-dom": "^16.8.3","@types/webpack-env": "1.13.1","ajv": "^5.2.2","gulp": "^3.9.1"
  }

解决方法

如果您希望SPFx使用不同版本的TypeScript,则需要更改@microsoft/rush-stack-compiler-3.3依赖性。 您可以选择here所需的TypeScript版本。

安装新的@microsoft/rusk-stack-compiler后,您必须在tsconfig.json的顶部进行更改。