In a TS project I'd like the following to be blocked:
- A file from folder
commonimporting from folderprojectA - A file from folder
projectBimporting from folderprojectA
I'd like the following to be allowed:
- A file from folder
projectAimporting from foldercommon.
I'm aware of References. However, as I understand, they require a build for type checking (If such a separation is made, one must build to create d.ts files first) which I'd rather avoid.
What options do I have? Is it possible to achieve simply via separate tsconfig files for each of those projects/folders?
I suggest to use a linter for that job, no need to adjust the build step or use Project References.
eslint-plugin-importis a quite popular ESLint plugin, compatible to TS and can do what you want. After having configured typescript-eslint (if not already done), you can play around with these rules:Let's try with following project structure:
.eslintrc.js:
Looking into file
./src/common/common.ts:The
import/no-relative-parent-importsrule also complains for both imports, like fora.ts:The third rule
import/no-internal-moduleswasn't used, but I also list it here, as it can be very useful to restrict access to child folders/modules and emulate (at least) some kind of package internal modifier in TS.