Abderrahim Benmelouka
Abderrahim Benmelouka

Reputation: 405

check library files in typescript but don't emit them

I'm working on an sub-module for an old school pure PHP project. This project has a folder for some JS dependencies.

For my sanity, I'm using Typescript in the sub-module but I'm having trouble with how to reference the dependencies and get partial type inference from the libraries in the dependencies folder.

Theses dependencies are local only and I can't change that for compatibility reasons, so I can't really use npm.

This is the project structure :

In my tsconfig.json, I have the following options :

{
    "compilerOptions": {
        // ...
        "allowJs": true,
        "checkJs": true,
        "rootDir": "./ts",
        "outDir": "./js",
        // ...
    },
    "include": [
        "./ts/**/*",
        "../dependencies/**/*.js"
    ],
}

This works pretty well. Typescript can resolve the types from the dependencies folder and correctly compiles my ./ts files into ./js. The only problem is that it keeps complaining with the following error for each file in ../dependencies :

error TS6059: File is not under 'rootDir'. 'rootDir' is expected to contain all source files.

I tried changing the 'rootDir' to ../. But this causes tsc to output all the dependencies into the ./js folder. I do not want that.

Upvotes: 0

Views: 33

Answers (1)

Lastik
Lastik

Reputation: 981

I have several points and advises for you

  • The TS6059 error correctly states that you should have all the source files under the root dir, so your change of rootDir was correct.

  • You shouldn't place js dependencies inside include array, because they are already js files.

  • The "allowJs": true options makes your js files compilable, so they will always be in output directory.

  • You shouldn't use js folder as an output directory by the way. In such case this directory has two purposes:

    • It stores input js files
    • It serves as an output dir for ts files.

I would advise you do make dist directory as an output directory instead of js directory. Separation of concern is the key. Input directory and output directory should differ.

I would also strongly advise you to use some bundler, like Webpack or Rollup for your goals instead of raw ts compiling.

Upvotes: 0

Related Questions