Typen 'IPromise<{}>' kan inte tilldelas typen 'IPromise' typescript 2.8.0
Jag använder AngularJS (1.5.8) ramverk med senaste TypeScript (2.8.0) filer. Följande kod kompileras inte efter uppdatering till den senaste TypeScript-versionen.
Angular-gränssnittsimplementering:
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-gränssnitt:
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;
}
Klassen som implementerar 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(",");
}
}
Jag får följande fel efter att ha uppdaterat till senaste TypeScript (2.8.0):
Typen 'IPromise<{}>' kan inte tilldelas typen 'IPromise'. Typen '{}' kan inte tilldelas typen 'TemplatesPagingModel'. Egenskapen 'templates' saknas i typen '{}'.
Svar #1
Försök ändra den här raden:
const defer = this.$q.defer();
Till detta:
const defer = this.$q.defer<TemplatesPagingModel>();
Som standard skapar this.$q.defer() ett IDeferred<{}>. IPromise-egenskapen hos det deferred-objektet (åtkomligt via defer.promise) tar automatiskt upp samma generiska typ som IDeferred, vilket orsakar det fel som du upplever. Du kan instruera TypeScript-kompilatorn att istället behandla detta som ett IDeferred<TemplatesPagingModel> genom att explicit lägga till denna generiska parameter.
