DamianEdwards / grunt-tsng

A TypeScript pre-processor for AngularJS
Apache License 2.0
29 stars 3 forks source link

Unable to resolve reference to ui-router $state service #9

Open Petteroe opened 9 years ago

Petteroe commented 9 years ago

I get an error when using tsng to compile one of my controllers after adding a dependency on the $state service from UI-Router:

Running "tsng:dev" (tsng) task
Warning: Error: Can't resolve dependency for controller MyPage.Features.HomePageController with name ng.ui.IState Use --force to continue.

Here is my controller:

/// <reference path="../../../../bower_components/dt-angular-ui/angular-ui.d.ts" />

module MyPage.Features {
    export interface IHomePageViewModel {
        loading: boolean;
        accounts: Array<Models.IAccount>;
        loadAccount(account: Models.IAccount, $event: BaseJQueryEventObject): void;
    }

    class HomePageController implements IHomePageViewModel {
        public loading: boolean;
        public accounts: Array<Models.IAccount>;

        constructor(private $state: ng.ui.IState, private $stateParams: ng.ui.IStateParams, private accountApiService: Apis.IAccountApiService) {
            this.loading = true;
            accountApiService.getAccounts().then(promiseValue => {
                this.accounts = promiseValue;
                this.loading = false;
            });
        }

        public loadAccount(account: Models.IAccount, $event: BaseJQueryEventObject): void {
            $event.stopPropagation();
            this.$state.go("account.load", {
                accountID: account.accountID
            });
        }
    }
}

Please note that the controller worked fine before I added the $state dependency. The $stateParams service is also part of UI-Router, and that is resolved correctly.

I am very new to using the tsng compiler so any help in resolving this would be greatly appreciated.

EDIT: The description above is slightly inaccurate. There was one more change I did: I added private modifiers on all the constructor parameters, and it turned out that that was the problem.

Petteroe commented 9 years ago

I found the issue. The parseConstructor method doesn't account for these auto-property modifiers, so when it builds the reference arrays, it always assume that each dependency has 2 tokens (name and type), when it is perfectly legal in typescript to have 3 tokens. TSNG should respect this feature of TypeScript and make sure to handle the public and private modifiers on constructor arguments properly.