I have a problem I cannot solve with typescript 4.0 to create a declaration file (d.ts) for a jsdoc annotated javascript library in commonJS format.
If one or more objects is required from more than a module when I run the typescript compiler to create the d.ts files I get this error:
index.js:1:1 - error TS9006: Declaration emit for this file requires using private name 'Rectangle' from module '"/Users/gabry/projects/ts-test/rectangle"'. An explicit type annotation may unblock declaration emit.
1 const {Rectangle} = require('./rectangle');
~~~~~
Found 1 error.
I can "fix" it by adding "export" before class in rectangle.js, but in that way the code is not more runnable under node.js...
I've seen there is a bug in typescript about something similar https://github.com/microsoft/TypeScript/issues/9865 , that should be solved in typescript 4.1 beta, but also installing that version of typescript I have the same problem, maybe something is wrong in the way I use commonJS require/module.exports?
Here is a sample:
index.js
const {Rectangle} = require('./rectangle');
class Render {
constructor() {
/**
* Object list
* @type {Rectangle[]}
*/
this.objects = [];
}
/**
* Adds a rectangle
*
* @returns {Rectangle} the rect
*/
addRectangle() {
const obj = new Rectangle();
this.objects.push(obj);
return obj;
}
}
module.exports = { Render }
rectangle.js
class Rectangle {
constructor() {
console.log("I'm a rectangle!");
}
}
module.exports = { Rectangle };
this is the tsconfig.json:
{
// Change this to match your project
"files": ["index.js"],
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "types",
"strict": false
}
}
I tried adding/changing tons of tsconfig.json params (target, module, moduleResolution...) without success...
I can run it without problems from a simple test script with node:
test.js
const {Render} = require("./index");
let render = new Render();
render.addRectangle();
console.log("Objects", render.objects);
node test.js
...returns as expected:
I'm a rectangle!
Objects [ Rectangle {} ]
This is now fixed in the released version of typescript 4.1, it was, indeed, a bug of the typescript compiler that occurred when emitting only declarations, the first betas of typescript 4.1 still had the bug, but a certain point the nightly began to be fixed (see the bug in the original question for detailed history) and eventually the final, working, v4.1 was released on the npm registry.
So if someone stills has this problem it can simply update his typescript dependency to the latest stable version ( ^4.1 ).