Angular 16升级时TypeScript createNodeArray弃用警告的消除方案
问题描述
我正在将Angular项目升级至最新稳定版Angular 16,同时需要同步升级TypeScript版本。若将TS版本升级至5.x.x并运行npm run -s tslint,会出现错误TypeError: ts.createNodeArray is not a function;若将TS版本降级至4.x.x,该错误则变为弃用警告:
DeprecationWarning: 'createNodeArray' has been deprecated since v4.0.0. Use the appropriate method on 'ts.factory' or the 'factory' supplied by your transformation context instead.
现咨询:需要做出哪些修改才能消除该弃用警告?
项目配置文件
package.json
{ "dependencies": { "@angular/animations": "16.2.8", "@angular/cdk": "16.2.7", "@angular/common": "16.2.8", "@angular/compiler": "16.2.8", "@angular/core": "16.2.8", "@angular/forms": "16.2.8", "@angular/localize": "16.2.8", "@angular/material": "16.2.7", "@angular/material-moment-adapter": "16.2.7", "@angular/platform-browser": "16.2.8", "@angular/platform-browser-dynamic": "16.2.8", "@angular/platform-server": "16.2.8", "@angular/router": "16.2.8", "@stomp/stompjs": "7.0.0", "core-js": "3.33.0", "diff-match-patch-ts": "0.3.0", "jquery": "3.7.1", "lodash": "4.17.21", "moment": "2.29.4", "net": "1.0.2", "ngx-diff": "5.0.0", "ngx-image-zoom": "2.1.0", "rxjs": "7.8.1", "sockjs-client": "1.6.1", "stompjs": "2.3.3", "zone.js": "0.13.3" }, "devDependencies": { "@angular-devkit/build-angular": "16.2.5", "@angular/cli": "16.2.5", "@angular/compiler-cli": "16.2.8", "@angular/language-service": "16.2.8", "@types/jasmine": "5.1.0", "@types/lodash": "4.14.199", "@types/node": "18.16.17", "codelyzer": "6.0.2", "jasmine-core": "5.1.1", "jasmine-spec-reporter": "7.0.0", "karma": "6.4.2", "karma-chrome-launcher": "3.2.0", "karma-cli": "2.0.0", "karma-coverage-istanbul-reporter": "3.0.3", "karma-jasmine": "5.1.0", "protractor": "7.0.0", "sass-lint": "1.13.1", "ts-node": "10.9.1", "tslint": "6.1.3", "typescript": "4.9.5" } }
tslint.json
{ "rulesDirectory": [ "node_modules/codelyzer" ], "rules": { "arrow-return-shorthand": true, "callable-types": true, "class-name": true, "comment-format": [ true, "check-space" ], "curly": true, // "deprecation": { // "severity": "warn" // }, "eofline": true, "forin": true, "import-blacklist": [ true, "rxjs/Rx" ], "import-spacing": true, "indent": [ true, "spaces" ], "interface-over-type-literal": true, "label-position": true, "max-line-length": [ true, 200 ], "member-access": false, "member-ordering": [ true, { "order": [ "static-field", "instance-field", "static-method", "instance-method" ] } ], "no-arg": true, "no-bitwise": true, "no-console": [ true, "debug", "info", "time", "timeEnd", "trace" ], "no-construct": true, "no-debugger": true, "no-duplicate-super": true, "no-empty": false, "no-empty-interface": true, "no-eval": true, "no-inferrable-types": [ true, "ignore-params" ], "no-misused-new": true, "no-non-null-assertion": true, "no-shadowed-variable": true, "no-string-literal": false, "no-string-throw": true, "no-switch-case-fall-through": true, "no-trailing-whitespace": true, "no-unnecessary-initializer": true, "no-unused-expression": true, "no-var-keyword": true, "object-literal-sort-keys": false, "one-line": [ true, "check-open-brace", "check-catch", "check-else", "check-whitespace" ], "prefer-const": true, "quotemark": [ false, "single" ], "radix": true, "semicolon": [ true, "always" ], "triple-equals": [ true, "allow-null-check" ], "typedef-whitespace": [ true, { "call-signature": "nospace", "index-signature": "nospace", "parameter": "nospace", "property-declaration": "nospace", "variable-declaration": "nospace" } ], "unified-signatures": true, "variable-name": false, "whitespace": [ true, "check-branch", "check-decl", "check-operator", "check-separator", "check-type" ], "directive-selector": [ true, "attribute", "camelCase" ], "component-selector": [ true, "element", "kebab-case" ], "no-output-on-prefix": true, "no-inputs-metadata-property": true, "no-outputs-metadata-property": true, "no-host-metadata-property": true, "no-input-rename": true, "no-output-rename": true, "use-lifecycle-interface": true, "use-pipe-transform-interface": true, "component-class-suffix": true, "directive-class-suffix": true } }
解决方案
核心原因
这个问题的根源是TSLint 6.x和Codelyzer 6.x已停止维护,它们的内部代码依赖了TypeScript中已废弃(TS 4.x)或移除(TS 5.x)的ts.createNodeArray API,导致版本不兼容。
步骤1:替换TSLint为ESLint(推荐长期方案)
Angular从v12开始官方推荐使用ESLint替代TSLint,TSLint已不再更新,无法适配TypeScript 5.x及后续版本。操作如下:
- 卸载旧的lint工具:
npm uninstall tslint codelyzer --save-dev - 安装Angular ESLint集成工具:
ng add @angular-eslint/schematics - 自动迁移TSLint规则到ESLint:
ng generate @angular-eslint/schematics:convert-tslint-to-eslint --remove-tslint-if-no-more-tslint-targets - 更新package.json中的lint脚本,替换原TSLint命令:
"scripts": { "lint": "ng lint" } - 运行
npm run lint验证,确认无警告或错误,按需调整.eslintrc.json中的规则配置。
步骤2:临时修复(不替换工具的权宜之计)
如果暂时无法切换到ESLint,只能继续使用TypeScript 4.x,并抑制弃用警告:
- 修改package.json中的lint脚本,添加
--no-deprecation参数:"scripts": { "lint": "node --no-deprecation node_modules/tslint/bin/tslint -c tslint.json 'src/**/*.ts'" } - 注意:此方法仅能掩盖警告,无法解决根本的兼容性问题,后续升级Angular或TypeScript仍会遇到报错,建议尽快迁移到ESLint。
内容的提问来源于stack exchange,提问作者Stefan
相关产品推荐
相关产品推荐

