3

In angular1, we define constants in this fashion,

angular.module('taskApp', [
  'ui.router',
  'angular-loading-bar',
  'ngMessages',
])
.constant('url', {
   BASE: 'http://localhost:3222',
   BASE_API: 'http://localhost:3222/v1'
});

then I can inject it into any module I want like so,

Auth.$inject = ['$http', 'url'];

function Auth($http, url)

Now using typescript, I'd like to do the same, I've seen this approach: Inject Angular Constants in TypeScript, which seems to be okay, but not the approach i'm looking for.

From my research

  static $inject = ["$resource", "url"];
  constructor(private $resource: ng.resource.IResourceService, private url: ng.IModule)

I'm using IModule, because it is the only hint from angular's tsd file

/**
 * Register a constant service, such as a string, a number, an array, an object or a function, with the $injector. Unlike value it can be injected into a module configuration function (see config) and it cannot be overridden by an Angular decorator.
 *
 * @param name The name of the constant.
 * @param value The constant value.
 */
constant(name: string, value: any): IModule;
constant(object: Object): IModule;

but can't find right property to retrieve my variables

enter image description here

1 Answer 1

2

One way to do it would be to define an interface that describes the constant.

interface URL_CONSTANTS {
   BASE: string,
   BASE_API: string
}

Then you'd be able to use that as the type for the injected constant.

static $inject = ["$resource", "url"];
constructor(private $resource: ng.resource.IResourceService, private url: URL_CONSTANTS)
Sign up to request clarification or add additional context in comments.

Comments

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.