我有一个服务,包含$ http我的函数返回一个延迟对象.
我的界面:
export interface MyServiceScope { get: ng.IPromise<{}>; }
我的课:
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; } }
编译失败,出现以下错误:
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<{}>'.
作为参考,这里是 DefinitelyTyped 的IPromise定义.该IQService.defer()
调用返回一个IDeferred
对象,然后deferred.promise
返回IPromise对象.
我不确定我是否在界面中使用了错误的定义,或者没有以相同的方式返回延迟对象.任何投入将不胜感激!
在您的界面中,您定义了一个属性get
,在服务实现中,它是一个函数get()
.你可能想要的是一个函数,所以界面应该是:
export interface MyServiceScope { get(): ng.IPromise<{}>; }