user2842288 user2842288 - 1 year ago 73
TypeScript Question

Error when exporting function that returns class: Exported variable has or is using private name

I'm getting the error

error TS4025: Exported variable 'UserApiClientModule' has or is using private name 'UserApiClient'.

in the following code:

export var UserApiClientModule = {
fromConfiguration: (configuration: Configuration) => {
providers: [
provide: BASE_PATH,
useValue: basePath
provide: Configuration,
useValue: configuration
imports: [
class UserApiClient { }

return UserApiClient;

I suspect the solution is to export the type UserApiClient somehow but I'm not sure how to do this since it's declared in a function.

Answer Source

The point here is:

Typescript tries to guess what is the returned type of all the public parts. And not only guess, but to really declare the UserApiClient as a part of returned type of the fromConfiguration call

And because we return something, which is not exported, is internal.. there is a problem. But we can easily return something else, e.g. some common interface ... or at least magical any

// change this
export var UserApiClientModule = {
  fromConfiguration: (configuration: Configuration) => {

// to that
export var UserApiClientModule = {                // below is the change
  fromConfiguration: (configuration: Configuration) : any => {

I would prefer to declare some common interface IHaveDynamicData ... as in fact in similar case shown here How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download