简体   繁体   English

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

[英]VSCode / tslint does not resolve paths in tsconfig.json

vscode is somehow not honoring my tsconfig.json (for a couple of weeks now, it has been different. eiter my bad or vscode update...) vscode 以某种方式不尊重我的 tsconfig.json(几个星期以来,它已经不同了。无论是我的错误还是 vscode 更新......)

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

在此处输入图片说明

Respectively in the Problems tab:分别在问题选项卡中:

Cannot find module '@foo-animation/animation-micro' or its corresponding type declarations.找不到模块“@foo-animation/animation-micro”或其相应的类型声明。 ts(2307) ts(2307)

Regular base paths (like @angular/core ) are properly resolved , just my 'custom' ones are the problem... 常规基本路径(如@angular/core已正确解决,只有我的“自定义”路径才是问题...

Compiling, Building, Running... all works like a charm. 编译、构建、运行......一切都像魅力一样。 So I believe from an angular/typescript-perspective everything is fine.所以我相信从角度/打字稿的角度来看一切都很好。 (Also, my fellow developers using IntelliJ have no issues…) So it seems to boild down to „telling vscode about it“.... :-/ (另外,我使用 IntelliJ 的开发人员没有问题......)所以它似乎归结为“告诉 vscode 关于它”......:-/

My tsconfig.json sits in the root-folder of the project.我的tsconfig.json位于项目的根文件夹中。 the only thing is, that I use another tsconfig.app.json , which includes above tsconfig.json .唯一的事情是,我使用了另一个tsconfig.app.json ,其中包括tsconfig.json

在此处输入图片说明

So is there a way to tell vscode where to look for it's tsconfig.json (to encourage parsing those paths) ?那么有没有办法告诉 vscode 在哪里寻找它的 tsconfig.json (鼓励解析这些路径)?

This SO question and this VSCode github issue might be related, but I still don't know what to do. This SO questionthis VSCode github issue可能相关,但我仍然不知道该怎么做。

I had a similar issue while setting up my repositories recently and I think adding this line to your vscode settings.json should help:我最近在设置存储库时遇到了类似的问题,我认为将此行添加到您的 vscode settings.json 应该会有所帮助:

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

If your tslint.json is not in the root you should add your own path.如果您的 tslint.json 不在根目录中,您应该添加自己的路径。

You could use the typeRoots and types compileOptions.您可以使用typeRootstypes compileOptions。

typeRoots by itself tells tsc where to find type definitions. typeRoots本身告诉 tsc 在哪里可以找到类型定义。 see typeRoots见类型根

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

Using types tells it whether there are specific ones that you would like to be resolved.使用types告诉它是否有您想要解析的特定types Any other types will be ignored.任何其他类型都将被忽略。 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)
],                

Note: Specifying typeRoots and types compileOptions will exclude types that are usually included by default, unless explicitly included.注意:指定typeRootstypes compileOptions 将排除通常默认包含的类型,除非明确包含。

Other helpful alternative, depending on your runtime setup and requirements, could be to use the rootDirs compileOption.根据您的运行时设置和要求,其他有用的替代方法可能是使用rootDirs compileOption。

rootDirs allows you to treat everything almost like an 'Über jar' in java (the idea of everything being available in one place). 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. */

Depending on your use case, you may not even need both tsconfig files anymore using these.根据您的用例,您甚至可能不再需要使用这两个tsconfig文件。

Also note, using extends overwrites the referenced tsconfig settings, so be careful that you haven't accidentally overwritten some of your paths.另请注意,使用extends覆盖引用的tsconfig设置,因此请注意不要意外覆盖某些路径。 That could also potentially cause issues.这也可能导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 路径在tsconfig.json中无法正常工作 - paths is not working in tsconfig.json as expected 无法使用 tsconfig.json 缩短导入路径 - Cannot shorten import paths with tsconfig.json Angular 在 tsconfig.json 中声明的库路径别名不起作用:找不到模块:错误:无法解析 - Angular Library paths alias declared in tsconfig.json not working: Module not found: Error: Can't resolve Angular/Typescript tsconfig.json 配置多个路径的路径别名 - Angular/Typescript tsconfig.json configure path alias with multiple paths 如何在tsconfig.json文件中为外部库配置路径属性 - How to configure paths property in tsconfig.json file for external libraries vsCode中缺少tsconfig.json文件和实验装饰器警告 - Missing tsconfig.json file and Experimental Decorators warning in vsCode 在 tsconfig.json 上使用路径无法使用 angular 解析 - using path on tsconfig.json doesn't resolve using angular Angular 9 tsconfig.json & tsconfig.app.json 路径未按预期工作 - Angular 9 tsconfig.json & tsconfig.app.json paths are not working as expected 未使用的 function 和 tsconfig.json 或 tslint 设置中的变量(角度) - Un-used function & variable in tsconfig.json or tslint setting (Angular) TSLint抱怨@Component的module.id“ tsconfig.json中未包含相应文件”(在WebStorm中) - TSLint complains @Component's module.id “Corresponding file not included in tsconfig.json” (in WebStorm)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM