输入&#39; IPromise&lt; {}&gt;&#39;不能分配类型&#39; IPromise <templatespagingmodel>&#39;打字稿2.8.0

时间:2018-02-27 14:00:21

标签: typescript

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

角度接口实现:

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>;
}

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;
}

实现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(",");
}
}

我在收到最新的打字稿(2.8.0)后出现以下错误:

&#39;输入&#39; IPromise&lt; {}&gt;&#39;不能分配类型&#39; IPromise&#39;。 输入&#39; {}&#39;不能分配给&#39; TemplatesPagingModel&#39;。属性&#39;模板&#39;在&#39; {}&#39;中缺少。

1 个答案:

答案 0 :(得分:7)

尝试更改此行:

const defer = this.$q.defer();

对此:

const defer = this.$q.defer<TemplatesPagingModel>();

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