I've been working on transitioning a project to TypeScript, and while most of the setup is done, I'm struggling with the final steps. My goal is to use this TypeScript project in a regular JavaScript project, so I understand that I need to generate d.ts files for my existing sources. Currently, all my sources are .js files, and we are planning a gradual migration to TS. The challenge lies in emitting declarations alongside the current exports/require statements.
Here's a simple example illustrating the issue:
mod1.js
class MyClass {
constructor(name) {
this.name = name;
}
}
module.exports = {
MyClass,
};
mod2.js
const mod1 = require('./mod1');
module.exports = {
MyClass: mod1.MyClass,
};
When attempting to export MyClass in mod2 to redirect the namespace from which one can access MyClass when consuming the project, I encounter
Declaration emit for this file requires using private name 'MyClass' from module '"mod1"'. An explicit type annotation may unblock declaration emit.ts(9006)
We have numerous redirects in our codebase, with sets of files containing various classes, and index.js files at each directory level defining the available items in that namespace. We also have UI elements instantiated as class instances allowing calls like:
const {app, appui} = require('our-cool-app');
app.feature1.doSomething();
appui.component.someButton.click();
Is there an easy solution to automatically generate d.ts files from the .js sources?