In a monorepo, I have two modules, ModuleA and ModuleB. In ModuleB, I want to use the types from ModuleA, but never actually import any values. This is because ModuleB may not be bundled with ModuleA, so it shouldn't import anything from it at runtime (in fact, ModuleA may not be present at runtime). From my understanding, this is the purpose of the import type ...
syntax, it only imports the compile time types but does nothing at runtime.
How to I configure Typescript to only allow imports of compile time types from ModuleA so that it is never imported in the compiled JavaScript?
In my tsconfig I currently have this:
"paths": {
"ModuleA": [
"../node_modules/ModuleA/dist/esm/index.d.ts"
]
}
But this obviously allows imports of anything, including runtime values.
From what I know of the tsconfig settings, what you're looking for doesn't exist. There are related settings about emission of imports such as verbatimModuleSyntax
(make imports preserved if not written as type imports), importsNotUsedAsValues
, preserveValueImports
(prevent tsc from removing imports that look unused), isolatedModules
(help make code work across different compilers), but none of those do what you're looking for.
You could write a script to check that all your imports from that module use type imports, and then (if you're using Git,) use that script in a pre-commit hook.