1

I am creating an app with TypeScript and AngularJS 1, and I have run into a problem with creating a constant and passing the constant to the other class. I have the following constant in my app:

module app{
       export class AUTH_EVENTS {
            static get Default():any {
                    return {
                    LOGIN_SUCCESS: 'AUTH_EVENTS:LOGIN_SUCCESS',
                    LOGIN_FAILED: 'AUTH_EVENTS:LOGIN_FAILED',
                    LOGOUT_SUCCESS: 'AUTH_EVENTS:LOGOUT_SUCCESS',
                    LOGOUT_FAILED: 'AUTH_EVENTS:LOGOUT_FAILED',
                    SESSION_TIMEOUT: 'AUTH_EVENTS:SESSION_TIMEOUT',
                    NOT_AUTHORIZED: 'AUTH_EVENTS:NOT_AUTHORIZED'
                };
            }
        }
 var app = getModule();
 app.constant("AUTH_EVENTS", AUTH_EVENTS.Default())
}

Which I try to access here:

module app{
    class auth{

        constructor(public $q: ng.IQService,
            public $state:angular.ui.IState, 
            public AUTH_EVENTS: AUTH_EVENTS){
        }

             responseError(response:any) {
                if (response.status === 401) {
                    console.log(this.AUTH_EVENTS.LOGIN_SUCCESS);
                } 
                return this.$q.reject(response);
        }


    }
}

The issue that I have, is that in

console.log(this.AUTH_EVENTS.LOGIN_SUCCESS) 

the LOGIN_SUCCESS is not defined.

Do you have any ideas why is this happening? Is there any issue with defining the constant, or is it the issue with the class auth. To be more specific, this is the error that I get when I compile TS into JS:

error TS2339: Property 'LOGIN_SUCCESS' does not exist on type 'AUTH_EVENTS'.

1 Answer 1

1

What about this definiton:

module app{ 
   export class AUTH_EVENTS {       
       LOGIN_SUCCESS= 'AUTH_EVENTS:LOGIN_SUCCESS';
       LOGIN_FAILED= 'AUTH_EVENTS:LOGIN_FAILED';
       LOGOUT_SUCCESS= 'AUTH_EVENTS:LOGOUT_SUCCESS';
       LOGOUT_FAILED= 'AUTH_EVENTS:LOGOUT_FAILED';
       SESSION_TIMEOUT= 'AUTH_EVENTS:SESSION_TIMEOUT';
       NOT_AUTHORIZED= 'AUTH_EVENTS:NOT_AUTHORIZED';
       static  Default() { return new AUTH_EVENTS(); }
    }
  var app = getModule();
  app.constant("AUTH_EVENTS", AUTH_EVENTS.Default())
}
Sign up to request clarification or add additional context in comments.

3 Comments

It works well. I managed to fix with by deleting the static Default, and set all the variables within the export class AUTH_EVENTS to public. Could you tell me if there are any benefits of setting the static Default over setting the variables to public?
I would say, that it depends on your preferences only. I do prefer public members of class, because it is easy to use as a type (that class).... ALSO, public is default access modifier if its value is omitted...
Thanks! Thats what I thought :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.