Closed
Description
I am using AngularJs and injecting a service, which is a TypeScript class in an external module, into a controller which is also a TypeScript class. Though I am specifically making a call to a service method, the TypeScript compiler will not include the external module in the define function unless I have a JavaScript call to moduleVariable;
There is no reason, if you are calling an instance method of an imported TS class, that the associated import statement should not be processed in the compiled code.
Service TS Definition
export interface IPurchaseOrderDto {
id: number;
dateCreated: Date;
description: string;
}
export interface IPurchaseOrderDtoPageable extends IPageable<IPurchaseOrderDto> {
}
export class PurchaseOrdersService {
private _serviceBase: string;
private _purchaseOrdersResource: any;
constructor(private $resource: ng.resource.IResourceService, private $location: ng.ILocationService) {
// todo: The service base would be set from a global configuration/service/singleton normally.
this._serviceBase = "http://private-a994d-testapi780.apiary-mock.com/";
var serviceTemplate: string = this._serviceBase + "PurchaseOrders/:purchaseOrderId";
this._purchaseOrdersResource = this.$resource<any>(serviceTemplate, { purchaseOrderId: "@id" });
}
public fetchAll(count?: number, pageNumber?: number): IPurchaseOrderDtoPageable {
// we're doing get instead of query because we are returning the total page count in addition to the records
// so the response is considered a single object.
return this._purchaseOrdersResource.get({ count, pageNumber });
}
public fetch(id: number): IPurchaseOrderDto {
return this._purchaseOrdersResource.get({ id: id });
}
}
angular.module("app").service("purchaseOrderService", PurchaseOrdersService);
Controller TS Definition
import poService = require("dataAccessServices/PurchaseOrder.service");
class PurchaseOrdersController implements IFeatureController {
public title: string = "Purchase Orders";
public purchaseOrders: poService.IPurchaseOrderDtoPageable;
constructor($scope: any, private purchaseOrderService: poService.PurchaseOrdersService) {
this.purchaseOrders = this.purchaseOrderService.fetchAll(10, 1); //Same result if not using "this"
}
}
angular.module("app").controller("PurchaseOrdersController", PurchaseOrdersController);
Generated Controller JS (define is missing dataAccessServices/PurchaseOrder.service)
define(["require", "exports"], function (require, exports) {
var PurchaseOrdersController = (function () {
function PurchaseOrdersController($scope, purchaseOrderService) {
this.purchaseOrderService = purchaseOrderService;
this.title = "Purchase Orders";
this.purchaseOrders = this.purchaseOrderService.fetchAll(10, 1);
}
PurchaseOrdersController.$inject = ["$scope", "purchaseOrderService"];
return PurchaseOrdersController;
})();
angular.module("app").controller("PurchaseOrdersController", PurchaseOrdersController);
});
//# sourceMappingURL=../../modules/PurchaseOrder/PurchaseOrders.controller.js.map
"Fixed" Controller (TSLint says "expected assignment or function call")
import poService = require("dataAccessServices/PurchaseOrder.service");
poService; // necessary to cause define function to generated by TypeScript compiler
class PurchaseOrdersController implements IFeatureController {
public title: string = "Purchase Orders";
public purchaseOrders: poService.IPurchaseOrderDtoPageable;
constructor($scope: any, private purchaseOrderService: poService.PurchaseOrdersService) {
this.purchaseOrders = this.purchaseOrderService.fetchAll(10, 1);
}
}
angular.module("app").controller("PurchaseOrdersController", PurchaseOrdersController);
"Fixed" Controller JS
define(["require", "exports", "dataAccessServices/PurchaseOrder.service"], function (require, exports, poService) {
poService; // necessary to cause define parameter to be generated by TypeScript compiler
var PurchaseOrdersController = (function () {
function PurchaseOrdersController($scope, purchaseOrderService) {
this.purchaseOrderService = purchaseOrderService;
this.title = "Purchase Orders";
this.purchaseOrders = this.purchaseOrderService.fetchAll(10, 1);
}
PurchaseOrdersController.$inject = ["$scope", "purchaseOrderService"];
return PurchaseOrdersController;
})();
angular.module("app").controller("PurchaseOrdersController", PurchaseOrdersController);
});
//# sourceMappingURL=../../modules/PurchaseOrder/PurchaseOrders.controller.js.map