You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exported variable <variable name> has or is using private name <private name> #6307
Reacted by santosh, Gayan Ranasinghe, Harry Jubb, ChouUn, Reyad Attiyat, Twois, Richard Nienaber, Rico Kahler, EugeneSnihovsky, Pablo Reyes and 12 more
the module is only exporting an instance of the class
the class doesn't define any static methods that would require its class to be called upon
In situations where singletons are required forcing the exposure of a constructor to the outside world could introduce opportunities where none are intended
If there are currently constructs in the language already that address the use case that I've outlined above to a class then I apologize for my ignorance and hope you will point me in the right direction. Otherwise, wouldn't it be advisable to eliminate this error which seems to be guessing at the intention of a potential user and instead allow undefined/unresolved class errors to be flagged when users actually attempt to reference the class?
Again, thank you for your response and have a very healthy and happy New Year.
Reacted by André Gomes, Romel Perez and Jesús Bernal
This error occurs when you use the --declaration flag because the compiler is trying to produce a declaration file that exactly matches the module you defined.
Let's say you have this code:
/// MyFile.tsclassTest{// ... other members ....constructor(publicparent: Test){}}exportlett=newTest("some thing");
To produce a declaration file, the compiler has to write out a type for t:
The member t has the type Test. The type Test is not visible because it's not exported, so we can't write t: Test.
In the very simplest cases, we could rewrite Test's shape as an object type literal. But for the vast majority of cases, this doesn't work. As written, Test's shape is self-referential and can't be written as an anonymous type. This also doesn't work if Test has any private or protected members. So rather than let you get 65% of the way through writing a realistic class and then start erroring then, we just issue the error (you're almost certainly going to hit later anyway) right away and save you the trouble.
Reacted by Huan Li, Zach Bjornson, Giedrius Grabauskas, Oliver Joseph Ash, Brett Epps, zjiekai, Olavo Rocha Neto, Reyad Attiyat, Dan Homola, Viktor and 20 more
Not sure if I should be asking this here, but this issue came up when I googled this error, I didn't quite feel like there was a solution, and to complicate matters further I've found other issues suggesting it once worked but now doesn't. So I'm trying to delve further into it and see if this issue might eventually describe the error case I'm hitting too.
src/index.ts(34,38): error TS4020: Extends clause of exported class 'Session' has or is using private name 'EventEmitter'.
So I'm importing EventEmitter:
import * as EventEmitter from "eventemitter3"
Then:
export default class Session extends EventEmitter {
And then, of course, hitting the above error. This issue led me to believe I should be exporting EventEmitter, but every export method I tried resulted in no change.
Further, I've found this issue which seems to imply that this is something entirely different. Here is a sample repo. I'm new enough to TS that I don't know whose issue that is to fix, but this issue seems to claim this error involves not exporting the superclass and nothing I've done fixes that. Everything else in my file compiles, and this only fails when I try to export my class and use its declarations.
Nolan Darilek (@ndarilek) can you file a new issue and provide a self containing repro. from a cursory look this looks like a bug, but i will have to look at the code to know for sure.
Here I don't see why the interface needs to be exported at all . The required interface i.e. ITest in the above example is being generated anyway in the declaration file and it is only being used in the TestComponent. So there should be no need of exporting the interface
Also what is the difference between exporting the interface when it is declared i.e.
export interface ITest {}
and exporting it as
interface ITest {}
export {ITest}
The latter seems to throw the has or is using a private name error too.
Reacted by Tanguy Krotoff, dave kraftsow, Frazer, Kirill Nikolaev and Xenia Siskaki
Why can't Test be put in the d.ts file, but not exported? E.g.
class Test {
constructor(public someting: any);
}
export let t: Test;
This behaviour surprised me when generating declaration files for react classes - it requires you to export the State interface, which should be an implementation detail, not in the public interface of the module.
Reacted by Daniel Hunt, Arne Maier, dave kraftsow, Matt Rollins, Ian Tan, Nachshon Schwartz, Mariusz Pilarczyk, Kevin Rocker, Kirill Nikolaev, Zhiling Chen and 8 more
I'm curious why every interface must be exported if referenced by any other exported object as well. With large react-redux projects it leads to an enormous proliferation of exported interfaces, each of which must have their own unique names.
It seems that it affects many people and the workaround impacts public interfaces of modules which is not ideal. On the other hand, apparently, fixing this shouldn't require major changes to the emitted declarations. The referenced unexported symbols do already get declared.
Minimal reproduction:
# a.ts
interface A {
m: string;
}
export function f(a: A): string {
return a.m;
}
$ tsc -d a.ts
a.ts(5,22): error TS4078: Parameter 'a' of exported function has or
is using private name 'A'.
Expected:
# a.d.ts
interface A {
m: string;
}
export function f(a: A): string {
return a.m;
}
Indeed, a user of a should be able to call f({m: "hello"}) (and typecheck against the unexported declaration), but not necessarily refer to the interface A directly by name.
Note that unexported types do get written to declaration files. E.g. if a.ts has only interface A {}, a.d.ts gets the unexported declaration interface A {}.
Reacted by Cameron Martin, Dmitry, Pedro H S Teixeira, Mick Dekkers, Yannick Stachelscheid, Xenia Siskaki, Yatharth Agarwal, Alexander Peters, Adam Pietrasiak, Elliot and 19 more
Activity
DanielRosenwasser commentedon Jan 1, 2016
It's because you're exporting
twhich usesTestwhich hasn't been exported. You can fix this by exporting the classTest.jeffschwartz commentedon Jan 1, 2016
Daniel Rosenwasser (@DanielRosenwasser) thanks for your quick reply. While I understand your response I feel this is still an error because:
If there are currently constructs in the language already that address the use case that I've outlined above to a class then I apologize for my ignorance and hope you will point me in the right direction. Otherwise, wouldn't it be advisable to eliminate this error which seems to be guessing at the intention of a potential user and instead allow undefined/unresolved class errors to be flagged when users actually attempt to reference the class?
Again, thank you for your response and have a very healthy and happy New Year.
RyanCavanaugh commentedon Jan 1, 2016
This error occurs when you use the
--declarationflag because the compiler is trying to produce a declaration file that exactly matches the module you defined.Let's say you have this code:
To produce a declaration file, the compiler has to write out a type for
t:The member
thas the typeTest. The typeTestis not visible because it's not exported, so we can't writet: Test.In the very simplest cases, we could rewrite
Test's shape as an object type literal. But for the vast majority of cases, this doesn't work. As written,Test's shape is self-referential and can't be written as an anonymous type. This also doesn't work ifTesthas any private or protected members. So rather than let you get 65% of the way through writing a realistic class and then start erroring then, we just issue the error (you're almost certainly going to hit later anyway) right away and save you the trouble.In general you shouldn't be using classes to implement a singleton pattern in TypeScript. See http://stackoverflow.com/questions/30174078/how-to-define-singleton-in-typescript
jeffschwartz commentedon Jan 2, 2016
Ryan Cavanaugh (@RyanCavanaugh) you are correct, I am generating d.ts files for my project.
ndarilek commentedon Apr 13, 2016
Not sure if I should be asking this here, but this issue came up when I googled this error, I didn't quite feel like there was a solution, and to complicate matters further I've found other issues suggesting it once worked but now doesn't. So I'm trying to delve further into it and see if this issue might eventually describe the error case I'm hitting too.
src/index.ts(34,38): error TS4020: Extends clause of exported class 'Session' has or is using private name 'EventEmitter'.So I'm importing
EventEmitter:import * as EventEmitter from "eventemitter3"Then:
export default class Session extends EventEmitter {And then, of course, hitting the above error. This issue led me to believe I should be exporting EventEmitter, but every export method I tried resulted in no change.
Further, I've found this issue which seems to imply that this is something entirely different. Here is a sample repo. I'm new enough to TS that I don't know whose issue that is to fix, but this issue seems to claim this error involves not exporting the superclass and nothing I've done fixes that. Everything else in my file compiles, and this only fails when I try to export my class and use its declarations.
Thanks.
mhegazy commentedon Apr 13, 2016
Nolan Darilek (@ndarilek) can you file a new issue and provide a self containing repro. from a cursory look this looks like a bug, but i will have to look at the code to know for sure.
nahushf commentedon Sep 26, 2016
Why does this error occur in react when used with typescript?
I have a component
TestComponentwhich uses an interface to define its props.and the generated declaration file is:
Here I don't see why the interface needs to be exported at all . The required interface i.e.
ITestin the above example is being generated anyway in the declaration file and it is only being used in theTestComponent. So there should be no need of exporting the interfaceAlso what is the difference between exporting the interface when it is declared i.e.
and exporting it as
The latter seems to throw the
has or is using a private nameerror too.Adding font copy to bundle task
olavorn commentedon Dec 11, 2016
try this:
export let t = <any>new Test("some thing");Works if you don't bother taking the Test type to your d.ts declarations.
aMarCruz commentedon Jun 12, 2017
I'm new to TypeScript, but using v2.3.4 this works for me:
I got this // pluggable-parsers.d.ts
...and this pluggable-parsers.js
Transpiled and ready to be processed by Rollup.
cameron-martin commentedon Sep 25, 2017
Why can't
Testbe put in the d.ts file, but not exported? E.g.This behaviour surprised me when generating declaration files for react classes - it requires you to export the
Stateinterface, which should be an implementation detail, not in the public interface of the module.dontsave commentedon Oct 14, 2017
I'm curious why every interface must be exported if referenced by any other exported object as well. With large react-redux projects it leads to an enormous proliferation of exported interfaces, each of which must have their own unique names.
rohitkandhal commentedon Oct 31, 2017
Came here to point that you might get this error because of in correct order in export statement. I was getting ` error for following code:
error TS4025: Exported variable 'Error' has or is using private name 'ErrorProps'.Solution Need to change the export order
nichtverstehen commentedon Jan 24, 2018
Should this issue be reopened?
It seems that it affects many people and the workaround impacts public interfaces of modules which is not ideal. On the other hand, apparently, fixing this shouldn't require major changes to the emitted declarations. The referenced unexported symbols do already get declared.
Minimal reproduction:
Expected:
Indeed, a user of
ashould be able to callf({m: "hello"})(and typecheck against the unexported declaration), but not necessarily refer to the interfaceAdirectly by name.Note that unexported types do get written to declaration files. E.g. if
a.tshas onlyinterface A {},a.d.tsgets the unexported declarationinterface A {}.Re-type `helper` from `@ember/component/helper` (#24986)