Working with AngularJS, I have created several asynchronous functions that all use the same signature, which is app.Domain.GenericModel.EntityBase (my generic model). Here is an example:
get(resource: string): ng.IPromise<app.Domain.GenericModel.EntityBase[]> {
var self = this;
var deferred = self.qService.defer();
self.httpService.get(resource).then(function (result: any) {
deferred.resolve(result.data);
}, function (errors) {
self.exception = new app.Exceptions.Model.Exception(errors.status, errors.statusText);
deferred.reject(self.exception);
});
return deferred.promise;
}
When attempting to call similar services with chained promises, I encountered the following error: "Type IPromise is not assignable to type IPromise, Type EntityBase is not assignable to type void"
var self = this;
var promise = self.$q.when();
promise = promise.then(() => {
return self.EcritureService.get(critureToSave);
}).then((compte: EntityBase) => {
return self.CompteService.getSingle(Number(data.compte));
}).then((EntityBase) => {
currentAccount.montantCpt = currentAccount.montantCpt + montant;
return self.CompteService.update(currentAccount:EntityBase);
});
I have researched extensively for a solution to this issue and have only come across a method to convert my functions' returns to the common pattern "IPromise" through Type assertion, which seems to rely on some trickery or deception by the compiler. If anyone has a better understanding of this technique, please share it regardless of its efficiency.