'IPromise<any>' 不可分配给类型 'Promise<any>'
'IPromise<any>' is not assignable to type 'Promise<any>'
我正在为用户更改选项卡时创建一个确认对话框ui。代码工作正常,但 tslint 抱怨类型不匹配。控制器看起来像:
export class MyController implements ng.IController {
constructor(private $transitions: TransitionService,
private $translate: angular.translate.ITranslateService) {
'ngInject';
$transitions.onStart({}, () =>
this.alert
.showConfirm(
this.$translate.instant('dialogs.confirmLeave.content'),
this.$translate.instant('dialogs.confirmLeave.title')
)
.then(() => true)
.catch(() => false)
);
}
...
}
this.alert.showConfirm
最终从 angular-ui-bootstrap 调用 this.$uibModal.open()
。这给出了以下警告:
error TS2345: Argument of type '() => IPromise<boolean>' is not assignable to parameter of type 'TransitionHookFn'.
Type 'IPromise<boolean>' is not assignable to type 'boolean | void | TargetState | Promise<boolean | void | TargetState>'.
Type 'IPromise<boolean>' is not assignable to type 'Promise<boolean | void | TargetState>'.
Types of property 'then' are incompatible.
Type '{ <TResult>(successCallback: (promiseValue: boolean) => TResult | IPromise<TResult>, errorCallbac...' is not assignable to type '<TResult1 = boolean | void | TargetState, TResult2 = never>(onfulfilled?: (value: boolean | void ...'.
Type 'IPromise<any>' is not assignable to type 'Promise<any>'.
Types of property 'then' are incompatible.
Type '{ <TResult>(successCallback: (promiseValue: any) => TResult | IPromise<TResult>, errorCallback?: ...' is not assignable to type '<TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1 | PromiseLike<TResult1>...'.
Type 'IPromise<any>' is not assignable to type 'Promise<any>'.
关于如何解决这个问题有什么想法吗?
看起来无论 this.alert.showConfirm()
是什么,它都没有返回真正的 Promise
。尝试将结果包装在 Promise
:
中
export class MyController implements ng.IController {
constructor(private $transitions: TransitionService,
private $translate: angular.translate.ITranslateService) {
'ngInject';
$transitions.onStart({}, () =>
Promise.resolve(
this.alert
.showConfirm(
this.$translate.instant('einsteinsst.dialogs.confirmLeave.content'),
this.$translate.instant('einsteinsst.dialogs.confirmLeave.title')
)
)
.then(() => true)
.catch(() => false)
);
}
...
}
Promise.resolve()
会将任何 thenable(即任何实现 IPromise
的东西转换成真正的 Promise
,但是 IPromise
接口不直接兼容 Promise
.
有关详细信息,请参阅 docs。
我正在为用户更改选项卡时创建一个确认对话框ui。代码工作正常,但 tslint 抱怨类型不匹配。控制器看起来像:
export class MyController implements ng.IController {
constructor(private $transitions: TransitionService,
private $translate: angular.translate.ITranslateService) {
'ngInject';
$transitions.onStart({}, () =>
this.alert
.showConfirm(
this.$translate.instant('dialogs.confirmLeave.content'),
this.$translate.instant('dialogs.confirmLeave.title')
)
.then(() => true)
.catch(() => false)
);
}
...
}
this.alert.showConfirm
最终从 angular-ui-bootstrap 调用 this.$uibModal.open()
。这给出了以下警告:
error TS2345: Argument of type '() => IPromise<boolean>' is not assignable to parameter of type 'TransitionHookFn'.
Type 'IPromise<boolean>' is not assignable to type 'boolean | void | TargetState | Promise<boolean | void | TargetState>'.
Type 'IPromise<boolean>' is not assignable to type 'Promise<boolean | void | TargetState>'.
Types of property 'then' are incompatible.
Type '{ <TResult>(successCallback: (promiseValue: boolean) => TResult | IPromise<TResult>, errorCallbac...' is not assignable to type '<TResult1 = boolean | void | TargetState, TResult2 = never>(onfulfilled?: (value: boolean | void ...'.
Type 'IPromise<any>' is not assignable to type 'Promise<any>'.
Types of property 'then' are incompatible.
Type '{ <TResult>(successCallback: (promiseValue: any) => TResult | IPromise<TResult>, errorCallback?: ...' is not assignable to type '<TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1 | PromiseLike<TResult1>...'.
Type 'IPromise<any>' is not assignable to type 'Promise<any>'.
关于如何解决这个问题有什么想法吗?
看起来无论 this.alert.showConfirm()
是什么,它都没有返回真正的 Promise
。尝试将结果包装在 Promise
:
export class MyController implements ng.IController {
constructor(private $transitions: TransitionService,
private $translate: angular.translate.ITranslateService) {
'ngInject';
$transitions.onStart({}, () =>
Promise.resolve(
this.alert
.showConfirm(
this.$translate.instant('einsteinsst.dialogs.confirmLeave.content'),
this.$translate.instant('einsteinsst.dialogs.confirmLeave.title')
)
)
.then(() => true)
.catch(() => false)
);
}
...
}
Promise.resolve()
会将任何 thenable(即任何实现 IPromise
的东西转换成真正的 Promise
,但是 IPromise
接口不直接兼容 Promise
.
有关详细信息,请参阅 docs。