在TypeScript中使用带有ES6 Promise的Thenable接口

时间:2018-02-14 09:58:43

标签: typescript es6-promise typescript-typings ajv

有些库提供Thenable接口类型f.e. 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

1 个答案:

答案 0 :(得分:2)

Ajv的Thenable类型声明表示then的第二个参数(通常称为onRejected)在调用时必须返回与第一个<U>相同的类型onFulfilled参数。 ES6承诺,因此TypeScript的Promise / PromiseLike没有这样的限制。

例如,在此代码中:

const p1: PromiseLike<boolean> = /* ... */
const p2 = p1.then(() => true, () => 123)

TypeScript将(正确地)推断出p2的类型为PromiseLike<number | boolean>。使用Ajv的Thenable类型声明,等效代码将无法编译,因为123不能分配给boolean。

实际的AJV JavaScript代码似乎正在返回正常的Promises,所以 它不关心类型。所以这似乎是AJV的TypeScript声明中的一个错误...我不知道为什么AJV没有在这里使用TypeScript的内置PromiseLike ...