4

We have the following config in our 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:

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

Problem comes while linting the project:

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

Any way to solve it without going back to using ./***/***/ for imports?

Deniss M.
  • 3,617
  • 17
  • 52
  • 100
  • You are evidently using a custom lint rule and that lint rule is broken. Disable the rule and file an issue – Aluan Haddad Sep 29 '18 at 19:49
  • 1
    @AluanHaddad custom lint rule? https://palantir.github.io/tslint/rules/no-implicit-dependencies/ this is the rule I think. – Deniss M. Sep 30 '18 at 09:19
  • 1
    Ah it's part of the official rules. Thanks for the reference. You can disable the rule or whitelist modules as it says. I still say that this is a bug in the rule though. In a number of situations, paths are not simply a convenience. – Aluan Haddad Sep 30 '18 at 09:52
  • @AluanHaddad whitelist modules? Can you please give an example :) Thanks and btw you can submit an answer instead of a comment. Also I have seen this discussion, which seems to follow a similar pattern: https://stackoverflow.com/questions/43281741/how-to-use-paths-in-tsconfig-json – Deniss M. Sep 30 '18 at 09:54
  • 1
    Well I don't think I answered any question since the document you yourself just linked shows how to configure the rule with a whitelist. All I have to add is my grumpy opinion that path mapping matters. – Aluan Haddad Sep 30 '18 at 09:57
  • 1
    @AluanHaddad I am trying to understand how to create a whitelist based on the doc I provided, but I cannot. Can you please give an example / hint? Thanks! Also an interesting topic: https://github.com/palantir/tslint/issues/3364 – Deniss M. Sep 30 '18 at 09:59

1 Answers1

4

You can configure the rule with a whitelist as documented here https://palantir.github.io/tslint/rules/no-implicit-dependencies

That will look like this:

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.

Personally, I think the rule should be smarter and attempt to parse a tsconfig for paths to some extent. Sometimes one has many paths and not everyone uses 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.

This should now work for @ prefixed paths as https://github.com/palantir/tslint/pull/4192 has been merged. Until you can upgrade you may need to use "app" and "pages".

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
  • 1
    @DenissM. Damn it looks like this wont work for `@` prefixed paths until https://github.com/palantir/tslint/pull/4192 is merged – Aluan Haddad Sep 30 '18 at 10:14
  • Yep, also got this impression while browsing their github... Well then we have two choices - rename all to app (without @) or switch back to regular imports. – Deniss M. Sep 30 '18 at 11:16
  • 1
    Or turn off the rule till they fix the damn thing. That's definitely what I would do. – Aluan Haddad Sep 30 '18 at 15:26