16

In my code I have this:

/// <reference path="../../../../scripts/typings/angularjs/angular.d.ts" />

angular.module('question')
    .controller('QuestionHomeController', [
        '$http',
        'enumService',
        '$q',
        '$rootScope',
        '$scope',
        '$state',
        questionHomeController]);

function questionHomeController(
    $http,
    enumService,
    $q,
    $rootScope,
    $scope,
    $state
    ) {

Can someone tell me how I can add Typescript typing for the $http and the $q? I already reference the AngularJS type definition files but the typings still do not show up

1 Answer 1

31

If you have the typings included in your project, you just need to use it as the type for the variables, so that you get the properties/functions on that type show up on the intellisense.

angular.module('question')
    .controller('QuestionHomeController', [
        '$http',
        'enumService',
        '$q',
        '$rootScope',
        '$scope',
        '$state',
        questionHomeController]);

function questionHomeController(
    $http : ng.IHttpService,
    enumService,
    $q: ng.IQService,
    $rootScope : ng.IRootScopeService,
    $scope : ng.IScope,
    $state : ng.ui.IStateService
    ) {

Similarly you can also specify the interface type for the service example for enumService. Also you can extend the existing types as well. For example your scope has its own function and you use scope as your viewModel then:-

interface IquestionHomeViewModel extends ng.IScope {
        func1(arg:string):void;
        ...

}

and you would use it as:-

$scope : IquestionHomeViewModel ,
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much. Can you tell me where I can get the ng.ui.IStateService. Everything works for me except this one.
@Alan oh what is $state? I thought is was angular ui router state service that you are using? If so you would need to download angular-ui-router.d.ts typing file for $state and any other ui router entities.
Yes. If you are using any other components other than core angular you would need to include its own typings.

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.