Return AngularJS $ q clause with TypeScript

I have a service that wraps $ http with my functions returning a pending object.

My interface:

export interface MyServiceScope { get: ng.IPromise<{}>; } 

My class:

 export class MyService implements MyServiceScope { static $inject = ['$http', '$log']; constructor(private $http: ng.IHttpService, private $log: ng.ILogService, private $q: ng.IQService) { this.$http = $http; this.$log = $log; this.$q = $q; } get(): ng.IPromise<{}> { var self = this; var deferred = this.$q.defer(); this.$http.get('http://localhost:8000/tags').then( function(response) { deferred.resolve(response.data); }, function(errors) { self.$log.debug(errors); deferred.reject(errors.data); } ); return deferred.promise; } } 

Compilation error with the following error:

 myservice.ts(10,18): error TS2420: Class 'MyService' incorrectly implements interface 'MyServiceScope'. Types of property 'get' are incompatible. Type '() => IPromise<{}>' is not assignable to type 'IPromise<{}>'. Property 'then' is missing in type '() => IPromise<{}>'. 

For reference, here is the DefinitionTyped IPromise definition . Calling IQService.defer() returns an IDeferred object, and then deferred.promise returns an IPromise object.

I am not sure if I am using the wrong definitions in my interface or not returning the pending object in the same way. Any input would be greatly appreciated!

+7
angularjs deferred typescript angular-promise
source share
1 answer

In your interface, you defined the get property, and in the service implementation, this is the get() function. Perhaps you need a function, so the interface should be:

 export interface MyServiceScope { get(): ng.IPromise<{}>; } 
+4
source share

All Articles