We are currently considering migrating a large JavaEE code base to TypeScript. Within this environment, there are multiple projects with intricate directory structures and JavaScript code that heavily relies on global variables across all projects.
Although TypeScript does offer multi-project support, as showcased in this small example on GitHub, our situation is more complex.
(1) The JS code is deeply embedded within the projects, for instance "Project1/src/main/resources/de/mycompany/webapp/ts". The resulting JS code should then be located at "Project1/src/main/resources/de/mycompany/webapp/ts-generated". TypeScript provides options like rootDir
and outDir
to handle this scenario.
(2) Due to the intricate folder structure, using relative imports is not feasible. For example, referencing a file from the 'core' project should have a consistent approach, regardless of whether it is accessed from the 'ui' or 'ui-utils' project. TypeScript introduces "baseUrl" and "paths" for absolute references, yet implementation seems challenging in a multi-project setup (or perhaps an error on our end). An example would be wanting to
import * from '@core/utils/mycode.ts'
, where this reference should align with the 'core' project's baseUrl or rootDir.
(3) There is a need to make connections to existing JS code in the global scope. TypeScript addresses this by providing declaration files (.d.ts) which can enhance the compiler's understanding through additional type information. Therefore, our plan involves generating declaration files for the current JS codebase, following by importing these declarations into consuming TypeScript projects. Nevertheless, we've encountered challenges where the import-keyword in TypeScript consistently imports declarations as a module with a namespace, despite the intention to link the JS objects to the global scope. We came across the concept of Triple-Slash import, which appears effective for relative references but lacks support for absolute references spanning different projects.
Questions
Regarding point (2): Is there a way to enable absolute imports across projects, preferably starting from baseUrl or rootDir?
Concerning point (3): Can TypeScript declarations from other projects be imported (or referenced) for JS code present in the global scope? It's crucial to note that the goal here isn't to generate code, but solely to inform the compiler about the existence of specific code.