Skip to content

FR: Warn when using await on PromiseLike #39387

Closed

Description

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:

PromiseLike await

Code

const promiseA = new Promise(resolve => {
  resolve(5);
})

async function main() {
  const a = await 5; // typescript will give warning 80007
  const b = await promiseA; // expected
  const c = await ({} as PromiseLike<number>); // The cast to PromiseLike silences 80007
}

Expected behavior:

There should be warning on the line with const c. I found this while using chai-as-promised. "await expect(promise).to.eventually.be(arg)" was throwing UnhandledPromiseRejectionWarning. Chai.PromisedAssertion is a PromiseLike and not a Promise (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/chai-as-promised/index.d.ts#L105) and it's not safe to use with await. But the type system hid that from me.

Actual behavior:

No warning.

Playground Link:

https://www.typescriptlang.org/play/index.html?ssl=10&ssc=2&pln=1&pc=1#code/MYewdgzgLgBADgJxAWwJYQKYEEYF4ZgYDuMACkmpgBQIYQgA2AbhngHwwDeAUDDLfWYYqAVgCUAbm4BfMd24BDCAE8wwGADMArmqipwMZAtRgqYrrxihIsBXhgKix2CKl9r0GACN7j5-Ap0bDcrcE91fD9UWCpOaQcIMkDMABlUAGsMAB4wLWQvDAQ2SXlpIA

Related Issues:

#35419

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

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions