Typescript prevent imports from certain directory in project
A few ideas based on some quick online research:
good-fences
is a dedicated tool to restrict imports in a TypeScript project. You'd have to add it to your build process as a separate step.- Compile your TypeScript code with
module
set toes6
(to a separate output directory if you need a differentmodule
setting to generate the code you actually run) and then run ESLint with theno-restricted-imports
rule on the output. - Set up both your runtime environment and your
tsconfig.json
so that you can use only non-relative imports, and then use theno-relative-imports
rule fromtslint-microsoft-contrib
. However, there was talk of deprecatingno-relative-imports
. - Write your own TSLint-based reimplementation of ESLint's
no-restricted-imports
and contribute it to tslint-eslint-rules.
It sounds like Dependency Cruiser is the tool you're looking for. You can put together any arbitrary rules you want with it, run it during your build process, and it'll automatically report dependency violations. It supports typescript, javascript, commonjs, es6 modules, and more.
I've actually been looking for an answer to this exact question for a while, and it wasn't until recently that I've stumbled upon this tool. Hopefully others can find it useful too.