简体   繁体   English

如何在没有 linting 错误的情况下配置角度项目 tsconfig paths[]?

[英]how to configure an angular project tsconfig paths[] without linting errors?

We have the following config in our tsconfig.ts我们的tsconfig.ts 中有以下配置

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@app/*": ["app/*"],
      "@pages/*": ["app/pages/*"]
      ...
    },

Then we can use much cleaner imports in our other ts files, something like this:然后我们可以在其他 ts 文件中使用更干净的导入,如下所示:

import {UrlConstants} from '@app/common/constants/url-constants';

Problem comes while linting the project: linting 项目时出现问题:

Module '@app/common' is not listed as dependency in package.json

Any way to solve it without going back to using ./***/***/ for imports?有什么方法可以解决它而无需返回使用./***/***/进行导入?

You can configure the rule with a whitelist as documented here https://palantir.github.io/tslint/rules/no-implicit-dependencies您可以使用此处记录的白名单配置规则https://palantir.github.io/tslint/rules/no-implicit-dependencies

That will look like this:看起来像这样:

tslint.json tslint.json

{
  "rules": {
    "no-implicit-dependencies": [
      true,
      [
        "app",
        "pages"
      ],
      "dev"
    ]
  }
}

The "dev" option isn't really for your scenario but it is useful if you lint your tests as I like to do. “dev”选项并不真正适合您的场景,但如果您按照我喜欢的方式整理测试,它会很有用。

Personally, I think the rule should be smarter and attempt to parse a tsconfig for paths to some extent.就个人而言,我认为规则应该更智能,并尝试在某种程度上解析路径的 tsconfig。 Sometimes one has many paths and not everyone uses NPM.有时一个人有很多路径,并不是每个人都使用 NPM。 JSPM users might have to just disable the rule which is a shame because the rule is very well motivated and very useful if you don't hit this rough edge. JSPM 用户可能不得不禁用该规则,这是一种耻辱,因为如果您没有达到这个粗略的边缘,该规则的动机非常好并且非常有用。

This should now work for @ prefixed paths as https://github.com/palantir/tslint/pull/4192 has been merged.这现在应该适用于@前缀路径,因为https://github.com/palantir/tslint/pull/4192已合并。 Until you can upgrade you may need to use "app" and "pages" .在升级之前,您可能需要使用"app""pages"

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

相关问题 angular 项目中的 tsconfig 和路径 - tsconfig and paths in angular project Angular/Typescript tsconfig.json 配置多个路径的路径别名 - Angular/Typescript tsconfig.json configure path alias with multiple paths 如何将 Angular 项目设置为在 linting 时始终发出错误而不是警告? - How do I set an Angular project to always emit errors instead of warnings when linting? 如何在tsconfig.json文件中为外部库配置路径属性 - How to configure paths property in tsconfig.json file for external libraries 如何配置 tsconfig.json 文件以包含角度库项目的 .ts 文件? - How to configure tsconfig.json file to include .ts files of an angular library project? 在 Angular 库中使用 tsconfig 路径? - Using tsconfig paths with Angular libraries? 如何在 vscode 中为角度项目设置正确的打字稿 linting? - How to setup proper typescript linting for an angular project in vscode? Angular tsconfig 无法访问路径中指定的模块 - Angular tsconfig not able to access the module specified in the paths 如何在angular2项目中创建tsconfig.json文件? - How to create tsconfig.json file in angular2 project? 如何在 jest.config.js 中设置 tsconfig 路径。 Angular 8. 开玩笑预设角度 - How to setup tsconfig paths in jest.config.js. Angular 8. jest-preset-angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM