1

I have a lib in a folder with subfolders, with some typescript files, and some of those depends of other files in other subfolders.

I want to include some of those files like it were local in another project. A similar function is available in visual studio (add reference file/folder), but was unable to reproduce with typescript/vscode.

Tried multiples ways like direct import and with the tsconfig (paths, includes, reference projects,etc) but all of those have problems to achieve that. I ended making symbolic links, but not the best solution.

Just to be clear:

  • I don't want to have a node_module/typings in each subfolder library
  • I don't want a precompiled project with each subfolder library
  • I just want a file in another folder like it were local in a project

Thanks in advance

Demo Repository

3
  • I was also going to suggest symbolic links but it sounds like you have already reached that conclusion. Have you considered using local modules as npm package depencies? Commented Mar 8, 2019 at 21:29
  • Your question is unclear. Please provide an example of a file structure. Commented Mar 8, 2019 at 22:11
  • Thanks @Wex, I've tried the npm links once, but caused some conflicts with webpack and the installation in other enviroments becomes complex, not to mention that the file needs to be compiled with his modules, etc. Maybe I asking too much... :) Commented Mar 8, 2019 at 22:27

1 Answer 1

1

If I understand correctly, "rootDirs" sounds like it accomplishes exactly what you are trying to achieve.

Using ‘rootDirs’, you can inform the compiler of the roots making up this “virtual” directory; and thus the compiler can resolve relative modules imports within these “virtual” directories as if were merged together in one directory.


Edit: Here's an update to your config to allow your node_modules to be shared with the projects in the lib folder. Note that you'll need to move your tsconfig to a parent directory that both these folders share, eg the root project directory:

        "baseUrl": ".",
        "paths": {
            "*": ["app/node_modules/*"]
        },
        "rootDirs": [
            "app/src",
            "lib",
        ]
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, tried that, but if o try to open the file (opening the symbol, etc) it complains about the missing types. I was able to make the code work sometimes, but the compiler starts to throw errors everywhere for missing modules (external modules), typings and dependencies. In visual studio (no vscode) it's possible to add a file like it exists in a local directory, at least in .net languages.
Added a demo repository. If you open the index in src and ctrl+click the Comp tag, it will open the file and complain about the missing react module.
A couple things to note: 1) Your react error goes away if you npm install in your app folder. 2) You can actually do import { Comp } from "./comp"; with the current rootDirs setup you have (this is what I meant when I said it gives you the feeling of having your lib/component folder be treated as though it were local).
Similarly, if you change rootDirs to ['src', '../lib'] your import would change to import { Comp } from "./component/comp";
Thanks again Wex. I followed your guide and I was able to import in different ways, and I have installed the react module with npm install, but still getting the error about the missing react in the comp.tsx. Typescript may generate valid code (even with the error), but keeps seeing the comp.tsx file where it is, and where it is don't have any module installed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.