Description
Bug Report
Declaring a constructor function like so:
const SomeConstructor = function() {
this.x = 1;
};
leads to typescript reporting error "TS9005": Declaration emit for this file requires using private name 'SomeConstructor'. An explicit type annotation may unblock declaration emit.
However the practically equivalent using a named function:
function SomeConstructor () {
this.x = 1;
};
works fine.
It seems to me that the TS compiler should not care which of these notations is used. But even if the difference is intentional, the error message generated seems incorrect or unhelpful and misleading at least. Also the hint to add type annotations seems inappropriate; no amount of explicit type annotations will make this error go away.
Similar errors get generated when using the following constructs:
module.exports = {
SomeConstructor: function () {
this.x = 1;
}
};
or
module.exports = function () {
this.x = 1;
};
My tsconfig.json:
{
"include": ["*.js"],
"compilerOptions": {
"allowJs": true,
"declaration": true,
"outDir": "dist",
"skipLibCheck": true
}
}
🔎 Search Terms
typescript TS9005
Declaration emit for this file requires using private name
typescript allowJs error 9005 const function class declaration
typescript allowJs error 9005 anonymous function class declaration
🕗 Version & Regression Information
This also happens in the Nightly version in the playground:
https://www.typescriptlang.org/play?strict=false&noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&ts=5.2.0-dev.20230727&filetype=js&strictNullChecks=false#code/MYewdgzgLgBAyiAtgUwMLmgJwK7CiTGAXhgDNsw8BLcGACgEoYBvAKAEgoALKiAOgAexGAEYA3KwC+EoA
It does not happen with versions 3.3.3-3.6.3:
https://www.typescriptlang.org/play?strict=false&noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&ts=3.6.3&filetype=js&strictNullChecks=false#code/MYewdgzgLgBAyiAtgUwMLmgJwK7CiTGAXhgDNsw8BLcGACgEoYBvAKAEgoALKiAOgAexGAEYA3KwC+EoA
However this versions also dont generate any typing information (.D.TS).
It seems to start happening with version 3.7.5:
https://www.typescriptlang.org/play?strict=false&noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&ts=3.7.5&filetype=js&strictNullChecks=false#code/MYewdgzgLgBAyiAtgUwMLmgJwK7CiTGAXhgDNsw8BLcGACgEoYBvAKAEgoALKiAOgAexGAEYA3KwC+EoA
- This changed between versions 3.6.3 and 3.7.5
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
Playground link with relevant code
💻 Code
const SomeConstructor = function() {
this.x = 1;
};
🙁 Actual behavior
tsc reports error "TS9005": Declaration emit for this file requires using private name 'SomeConstructor'. An explicit type annotation may unblock declaration emit.
This seems wrong because the equivalent code
function SomeConstructor () {
this.x = 1;
};
works fine and it seems that there shouldn't be any relevant differences between these notations for the TS compiler.
🙂 Expected behavior
tsc should be happy