类型';IPromise<;{}>';不可分配给类型';IPromise<;模板间距模型>';typescript 2.8.0

类型';IPromise<;{}>';不可分配给类型';IPromise<;模板间距模型>';typescript 2.8.0,typescript,Typescript,我正在使用angularjs(1.5.8)框架和最新的typescript(2.8.0)文件。以下代码在更新到最新的typescript版本后不会编译 角度接口实现: interface IPromise<T> { /** * Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asyn

我正在使用angularjs(1.5.8)框架和最新的typescript(2.8.0)文件。以下代码在更新到最新的typescript版本后不会编译

角度接口实现:

interface IPromise<T> {
    /**
     * Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
     * The successCallBack may return IPromise<void> for when a $q.reject() needs to be returned
     * This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback. It also notifies via the return value of the notifyCallback method. The promise can not be resolved or rejected from the notifyCallback method.
     */
    then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>|TResult, errorCallback?: (reason: any) => any, notifyCallback?: (state: any) => any): IPromise<TResult>;

    /**
     * Shorthand for promise.then(null, errorCallback)
     */
    catch<TResult>(onRejected: (reason: any) => IPromise<TResult>|TResult): IPromise<TResult>;

    /**
     * Allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.
     *
     * Because finally is a reserved word in JavaScript and reserved keywords are not supported as property names by ES3, you'll need to invoke the method like promise['finally'](callback) to make your code IE8 and Android 2.x compatible.
     */
    finally(finallyCallback: () => any): IPromise<T>;
}
接口IPromise{
/**
*无论承诺何时或将何时得到解决或拒绝,只要结果可用,就会异步调用其中一个成功或错误回调。回调使用一个参数进行调用:结果或拒绝原因。此外,notify回调可能被调用零次或多次,以提供进度指示n、 在承诺被解决或拒绝之前。
*当需要返回$q.reject()时,successCallBack可能会返回IPromise
*此方法返回一个新的承诺,该承诺通过successCallback的返回值errorCallback进行解析或拒绝。它还通过notifyCallback方法的返回值进行通知。无法通过notifyCallback方法解析或拒绝该承诺。
*/
然后(successCallback:(promiseValue:T)=>IPromise | TResult,errorCallback?:(原因:any)=>any,notifyCallback?:(状态:any)=>any):IPromise;
/**
*promise.then的缩写(null,errorCallback)
*/
catch(on拒绝:(原因:any)=>IPromise | TResult):IPromise;
/**
*允许您观察承诺的履行或拒绝,但不修改最终值。这有助于释放资源或进行清理,无论承诺是否被拒绝或解决。有关详细信息,请参阅完整规范。
*
*因为finally在JavaScript中是一个保留字,ES3不支持保留关键字作为属性名,所以您需要调用promise['finally'](回调)等方法,以使代码IE8和Android 2.x兼容。
*/
最后(最终回调:()=>any):IPromise;
}
ITemplate接口:

export interface ITemplateService {
/**
 * Scroll to 
 * @param inputFieldId 
 * @param offset 
 * @param ms 
 * @returns {} 
 */
getTemplates(dto?: TemplateFilterPostDto): ng.IPromise<TemplatesPagingModel>;

/**
 * Set template groups as comma seperated for list presentation
 * @param template The template to use
 */
setTemplateGroupsAsCommaSeperated(template: TemplateModel): void;
}
导出接口服务{
/**
*滚动到
*@param inputFieldId
*@param偏移量
*@param-ms
*@returns{}
*/
getTemplates(dto?:TemplateFilterPostDto):ng.IPromise;
/**
*将列表显示的模板组设置为逗号分隔
*@param template要使用的模板
*/
SetTemplateGroupsCommaseOperated(模板:TemplateModel):void;
}
实现ITemplateService的类:

export default class TemplateService implements ITemplateService {

public static $inject = ["TemplateRepository", "$q", "MappingService"];

private initialTemplateFilter: ITemplateFilterPostDto = {
    currentPage: 1,
    group: null,
    name: null,
    totalResults: ConfigurationService.TOTAL_ROWS[0]
};

constructor(
    public templateRepository: ITemplateRepository,
    public $q: ng.IQService,
    public mappingService: IMappingService) {
}

public getTemplates(dto?: ITemplateFilterPostDto): ng.IPromise<TemplatesPagingModel> {        
    const defer = this.$q.defer();

    this.templateRepository.getTemplates(dto ? dto : this.initialTemplateFilter, ConfigurationService.templatesFields).then((result) => {

            const templatesWithTotal = this.mappingService.validate<TemplatesPagingModel>(result.data, "TemplatesPagingDto", "[]");

            _.forEach(templatesWithTotal.templates,
                (template) => {
                    this.setTemplateGroupsAsCommaSeperated(template);
                });

            defer.resolve(templatesWithTotal);

        }, () => {
            defer.reject();
        });
    return defer.promise;
}

public setTemplateGroupsAsCommaSeperated(template: TemplateModel): void {
    template.groupsCommaSeparated = template.groups.map((g) => { return g.name; }).join(",");
}
}
导出默认类TemplateService实现ITemplateService{
公共静态$inject=[“TemplateRepository”、“$q”、“MappingService”];
私有initialTemplateFilter:ITemplateFilterPostDto={
当前页面:1,
组:空,
名称:空,
totalResults:ConfigurationService.TOTAL_行[0]
};
建造师(
公共模板存储库:ITemplateRepository,
公共$q:ng.IQ服务,
公共地图服务:IMappingService){
}
公共getTemplates(dto?:ITemplateFilterPostDto):ng.IPromise{
const defer=this.$q.defer();
this.templateRepository.getTemplates(dto?dto:this.initialTemplateFilter,ConfigurationService.templatesFields)。然后((结果)=>{
const templatesWithTotal=this.mappingService.validate(result.data,“TemplatesPagingDto”,“[]”);
_.forEach(templatesWithTotal.templates,
(模板)=>{
此.settemplateGroupsCommaseOperated(模板);
});
延迟解决(模板总数);
}, () => {
defer.reject();
});
回报、承诺;
}
public SetTemplateGroupsCommaseOperated(模板:TemplateModel):无效{
template.groupsCommaSeparated=template.groups.map((g)=>{return g.name;}).join(“,”;
}
}
在使用最新的typescript(2.8.0)后,我遇到以下错误:

“类型”IPromise“不可分配给类型”IPromise“。
类型“{}”不可分配给类型“TemplatesPagingModel”。类型“{}”中缺少属性“templates”

尝试更改此行:

const defer = this.$q.defer();
为此:

const defer = this.$q.defer<TemplatesPagingModel>();
const defer=this.$q.defer();
默认情况下,
this.$q.defer()
创建一个
IDeferred
。延迟对象的
IPromise
属性(通过
defer.promise
访问)自动拾取与
IDeferred
相同的泛型类型参数,这会导致您遇到的不匹配。通过显式添加此泛型参数,可以指示TypeScript编译器将其视为
IDeferred