title: TypeScript interceptor in AngularJS
body: <p>I'm having problems setting up a request interceptor in AngularJS using TypeScript</p>
  
  <p>The following snippet works, not working variant is commented out. No matter what I inject in the constructor the local variables are undefined in the <code>request</code> method.</p>
  
  <pre><code>module Services
  {
      export class AuthInterceptor
      {
          public static Factory(TokenService: Services.ITokenService)
          {
              return new AuthInterceptor(TokenService);
          }
  
          constructor(private TokenService: Services.ITokenService)
          {
              this.request = (config: ng.IRequestConfig) =&gt;
              {
                  config.headers = config.headers || {};
                  if(this.TokenService.IsAuthorised())
                      config.headers.Authorization = 'Bearer ' + this.TokenService.Token;
                  return config;
              };
          }
  
          public request: (config: ng.IRequestConfig)=&gt;ng.IRequestConfig;
  
  /* THIS IS NOT WORKING
  
          public request(config)
          {
                      // this.TokenService is undefined here as well as $window or $q which I tried to inject
              config.headers = config.headers || {};
              if(this.TokenService.Token != "")
                  config.headers.Authorization = 'Bearer ' + this.TokenService.Token;
              return config;
          }
  */
  
      }
  }
  
  angular.module("Services")
      .config(($httpProvider: ng.IHttpProvider)=&gt;
      {
          $httpProvider.interceptors.push(Services.AuthInterceptor.Factory);
      });
  </code></pre>