【发布时间】:2020-01-27 02:05:18
【问题描述】:
我正在进行一些 Angular 测试,但我的规范文件无法识别我的路径,并且它们在 VS Code 中给了我一个红色波浪线导入警告(并显示在问题中)尽管它们以其他方式工作(测试作品等)。我假设这是一个 tsconfig 问题,而不是一个 linting 问题,因为它给我的错误是: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
【问题讨论】:
-
确保您的
tsconfig.spec.json扩展了您提供paths的配置。或者将这些路径放在这个配置中 -
如上所示,` "extends": "../tsconfig.json",` 我正在扩展它。此外,我尝试将路径直接放入;好像没什么区别。
-
如果问题只是关于 VSCode 并且尽管一切正常,那么值得在 VSCode github 中创建一个问题
-
@MapLion 将您的打字稿版本降级为
typescript": "3.4.5" -
@Sergey 我不认为这是 VSCode 问题;我认为这是一个 typescript/tsconfig 问题,但我想先联系社区,然后再用比任何事情都更烦人的东西来解决问题。
标签: angular typescript testing visual-studio-code jestjs