一些库提供Thenable
接口类型 fe AJV。
我对他们有些不理解。鉴于这个最小的代码
const foo: Ajv.Thenable<boolean> = new Promise<boolean>((resolve, reject) => {
if ("condition")
resolve(true)
reject("Nope")
})
TypeScript 编译器抛出一个我无法理解的错误。
error TS2322: Type 'Promise<boolean>' is not assignable to type 'Thenable<boolean>'.
Types of property 'then' are incompatible.
Type '<TResult1 = boolean, TResult2 = never>(onfulfilled?: ((value: boolean) => TResult1 | PromiseLike<...' is not assignable to type '<U>(onFulfilled?: ((value: boolean) => U | Thenable<U>) | undefined, onRejected?: ((error: any) =...'.
Types of parameters 'onfulfilled' and 'onFulfilled' are incompatible.
Type '((value: boolean) => U | Thenable<U>) | undefined' is not assignable to type '((value: boolean) => U | PromiseLike<U>) | null | undefined'.
Type '(value: boolean) => U | Thenable<U>' is not assignable to type '((value: boolean) => U | PromiseLike<U>) | null | undefined'.
Type '(value: boolean) => U | Thenable<U>' is not assignable to type '(value: boolean) => U | PromiseLike<U>'.
Type 'U | Thenable<U>' is not assignable to type 'U | PromiseLike<U>'.
Type 'Thenable<U>' is not assignable to type 'U | PromiseLike<U>'.
Type 'Thenable<U>' is not assignable to type 'PromiseLike<U>'.
Types of property 'then' are incompatible.
Type '<U>(onFulfilled?: ((value: U) => U | Thenable<U>) | undefined, onRejected?: ((error: any) => U | ...' is not assignable to type '<TResult1 = U, TResult2 = never>(onfulfilled?: ((value: U) => TResult1 | PromiseLike<TResult1>) |...'.
Types of parameters 'onFulfilled' and 'onfulfilled' are incompatible.
Type '((value: U) => TResult1 | PromiseLike<TResult1>) | null | undefined' is not assignable to type '((value: U) => TResult2 | Thenable<TResult2>) | undefined'.
Type 'null' is not assignable to type '((value: U) => TResult2 | Thenable<TResult2>) | undefined'.
编译器究竟认为TypeScripts ES6 Promise会在哪里返回null
(如果那是实际错误)?
为什么有些库(bluebird、rsvp、ember、...)使用Thenable
而不是Promise
/ PromiseLike
?