我正在进行一些Angular测试,我的规格文件无法识别我的路径,尽管它们以其他方式工作(测试有效,等等),但它们在VS Code中给了我一个红色的花样导入警告(并显示在“问题”中)。 )。我假设这是tsconfig问题,而不是棉绒问题,因为它给我的错误是:Cannot find module '@feature/<reference path>.<file type>'.ts(2307)
它在功能上对我影响不大,但很烦人(并杀死了自动导入功能)。
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"module": "esnext",
"outDir": "./dist/out-tsc",
"strictNullChecks": true,
"noImplicitAny": false,
"noImplicitThis": true,
"alwaysStrict": false,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowJs": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"target": "es2015",
"types": [
"node",
"arcgis-js-api",
"jest"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom",
"esnext.asynciterable"
],
"baseUrl": ".",
"paths": {
"core-js/es6/*": [
"node_modules/core-js/es/*"
],
"core-js/es7/reflect": [
"node_modules/core-js/proposals/reflect-metadata"
],
"@core/*": [
"src/app/core/*"
],
"@shared/*": [
"src/app/shared/*"
],
"@feature/*": [
"src/app/feature/*"
],
"@test/*": [
"src/test/*"
],
"@/*": [
"src/*"
]
}
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"**/*.d.ts"
]
}
tsconfig.spec.json和tsconfig.app.json路径(请注意angular-cli中的标准文件夹结构):
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec"
}
}
angular.json
...
"test": {
"builder": "@angular-builders/jest:run",
"options": {
"tsConfig": "src/tsconfig.spec.json",
"no-cache": true,
"polyfills": "src/polyfills.ts",
"configPath": "./jest.config.js",
"styles": [
"node_modules/font-awesome/css/font-awesome.css",
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets",
"src/manifest.json",
],
"stylePreprocessorOptions": {
"includePaths": [
"src"
]
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
...
版本:
Angular CLI: 8.3.6
Node: 12.3.1
OS: win32 x64
Angular: 8.2.8
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.803.6
@angular-devkit/build-angular 0.803.6
@angular-devkit/build-optimizer 0.803.6
@angular-devkit/build-webpack 0.803.6
@angular-devkit/core 8.3.6
@angular-devkit/schematics 8.3.6
@angular/cli 8.3.6
@angular/pwa 0.803.6
@ngtools/webpack 8.3.6
@schematics/angular 8.3.6
@schematics/update 0.803.6
rxjs 6.5.3
typescript 3.5.3
webpack 4.39.2
最佳答案
我想这个问题与您的files
和include
配置有关。当我添加了相当有限的files
和include
模式集时,我立即看到了您描述的相同问题,原因很简单,即我的文件不是全部都匹配*.d.ts
。
要查看这是否是引起您问题的原因(或者如果您知道您的文件与*.d.ts
不匹配),我建议您首先删除files
和include
部分,保存tsconfig.json
并在VS中重新启动TS服务器码。
对于那些不知道的内容,可以通过打开.ts
文件中的命令面板(Ctrl + Shift + P或⌘+ Shift + P或仅F1)并键入Restart TS
并选择该命令,以VS Code重启TS服务器。
如果这是适合您的解决方案,那么您只需考虑files
和include
中的实际需求。 documentation应该在这方面有所帮助。
最大的收获:您不需要它们中的任何一个:
如果确实需要这种特殊性,则可能只需要在*.ts
模式中添加*.tsx
和/或include
模式即可解决此问题。
希望对您有所帮助!