Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS] Thenable vs. PromiseLike #717

Closed
krenor opened this issue Feb 22, 2018 · 5 comments
Closed

[TS] Thenable vs. PromiseLike #717

krenor opened this issue Feb 22, 2018 · 5 comments

Comments

@krenor
Copy link
Contributor

krenor commented Feb 22, 2018

If I understand it correctly Thenable is nothing more than a disguised Promise, right? Is there a reason why some libraries use Thenable rather than the official typescript Promise declaration?

Using Thenable is not possible with native Promises.

import * as Ajv from "ajv"

const foo: Ajv.Thenable<boolean> = new Promise<boolean>((resolve, reject) => {
  if ("condition")
    resolve(true)

  reject("Nope")
})

Will always compile with an error:

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'

Removing the Thenable Interface and replacing references with PromiseLike in the typings get rid of the error. Am I safe to provide a PR which does that or is there a reason behind Thenable I seem to miss?

/cc @blakeembrey

@blakeembrey
Copy link
Collaborator

blakeembrey commented Feb 22, 2018

Sounds reasonable. There’s only two reasons for the custom promise to exist:

  1. Supporting old TypeScript versions before the Promise type (like really old now)
  2. Supporting ES5 compilation without requiring promises

@ajv-validator ajv-validator deleted a comment from Bgshanoams Feb 22, 2018
@ajv-validator ajv-validator deleted a comment from Bgshanoams Feb 22, 2018
@epoberezkin
Copy link
Member

@blakeembrey thank you. Does it mean that should be a major version change?

@krenor
Copy link
Contributor Author

krenor commented Feb 23, 2018

@blakeembrey What exactly do you mean?

with requiring promises

I'm using TS most of the time compiling down to ES5 without any adjustments necessary to use Promises.

@blakeembrey
Copy link
Collaborator

@krenor Phone typo, sorry. Was meant to say without. But TypeScript ported PromiseLike to the old definitions without Promise a while back anyway to support some browser APIs IIRC (https://github.com/Microsoft/TypeScript/blob/32c63a26284c0fcf6dfbdce709f3da680c2940d9/lib/lib.es5.d.ts#L1298) so this isn't a concern, it's a good change since they did that.

@epoberezkin I don't think so. Pretty sure the version support is the same as other features your using, so all good.

@epoberezkin
Copy link
Member

Released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants