VSCode / tslint无法解析tsconfig.json中的路径

问题描述

vscode不能以某种方式兑现我的tsconfig.json(现在已经有几个星期了,它已经不一样了。后来我发现错误或vscode更新...)

{
"compileOnSave": false,"compilerOptions": {
    "baseUrl": "./","paths": {
        "@foo-animation/*": [
            "src/app/animation/*"
        ],...

enter image description here

分别在“问题”标签中:

找不到模块'@ foo-animation / animation-micro'或其对应的类型声明。 ts(2307)

常规基本路径(例如@angular/core)已被正确解决,只是我的“自定义”问题...

编译,构建,运行...所有工作都像灵符。因此,我相信从角度/打字稿角度来看,一切都很好。 (此外,使用IntelliJ的我的其他开发人员也没有问题……)因此,它似乎可以归结为“告诉vscode对此问题” ....:-/

我的tsconfig.json位于项目的根文件夹中。唯一的是,我使用了另一个tsconfig.app.json,其中包括tsconfig.json以上。

enter image description here

那么有没有办法告诉vscode在哪里寻找tsconfig.json(以鼓励解析那些路径)?

This SO questionthis VSCode github issue可能有关联,但我仍然不知道该怎么办。

解决方法

最近在设置存储库时遇到了类似的问题,我认为将此行添加到您的vscode settings.json应该会有所帮助:

"tslint.configFile": "./tslint.json"

如果tslint.json不在根目录中,则应添加自己的路径。

,

您可以使用 typeRootstypes compileOptions。

typeRoots 本身告诉 tsc 在哪里可以找到类型定义。 see typeRoots

"typeRoots": [
      "dirPath/@angular","node_modules/@types","node_modules/electron"
]                     /* List of folders to include type definitions from. */

使用 types 告诉它是否有您想要解决的特定问题。任何其他类型都将被忽略。 see types

    "types": [
      "animation-micro","jquery","node","react","prop-types","." //referencing types belonging to paths specified in typeRoots i.e. 'dirPath/@angular/.','node_modules/@types/.' &  'node_modules/electron/.' - needed to resolve namespaces (dependencies)
],

注意:指定 typeRootstypes compileOptions 将排除通常默认包含的类型,除非明确包含。

根据您的运行时设置和要求,其他有用的替代方法可能是使用 rootDirs compile Option。

rootDirs 允许您将所有内容几乎视为 Java 中的“Über jar”(所有内容都在一个地方可用的想法)。 see rootDirs

"rootDirs": [
      "/ts/","/js/","src/main/resources/static/ts","src/main/resources/static/js"
 ],/* List of root folders whose combined content represents the structure of the project at runtime. */

根据您的用例,您甚至可能不再需要使用这两个 tsconfig 文件。

另请注意,使用 extends overwrites 引用的 tsconfig 设置,因此请注意不要意外覆盖某些路径。这也可能导致问题。