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

Awaited has issues with custom Promise subtype #46934

Closed
DanielRosenwasser opened this issue Nov 27, 2021 · 0 comments · Fixed by #46951
Closed

Awaited has issues with custom Promise subtype #46934

DanielRosenwasser opened this issue Nov 27, 2021 · 0 comments · Fixed by #46951
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@DanielRosenwasser
Copy link
Member

Hello everyone,

Not sure if this is the correct place to bring this up but the Awaited<T> type as-is broke our build after upgrading from typescript 4.4.3 to 4.5.2.

For reference, here is what Awaited<T> looks like:

/**
 * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`.
 */
type Awaited<T> =
    T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
        T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
            F extends ((value: infer V) => any) ? // if the argument to `then` is callable, extracts the argument
                Awaited<V> : // recursively unwrap the value
                never : // the argument to `then` was not callable
        T; // non-object or non-thenable

The problem is that we have a custom Promise class (AbortablePromise) and its then function accepts a onfulfilled callback that do not exactly match the condition F extends ((value: infer V) => any).

Our then is declared like this:

  public then<TResult1 = T, TResult2 = never>(
    onfulfilled?: ((value: T, abortController: AbortController) => TResult1 | PromiseLike<TResult1>) | undefined | null,
    onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
  ): AbortablePromise<TResult1 | TResult2>;

So onfulfilled in our case is (value: T, abortController: AbortController) => TResult1 | PromiseLike<TResult1>) which does not match (value: infer V) => any

Hence, Awaited<T> where T is an AbortablePromise always returns never.

Would it be possible to make the type condition more flexible in Awaited<T> to enable these promise extensions and still have the type be compatible with Promise.all, Promise.race, etc?

Originally posted by @julienavert in #45350 (comment)

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Nov 29, 2021
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Fix Available A PR has been opened for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Dec 7, 2021
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.5.3 milestone Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants