Skip to content

Deeper inference of Promise types. #49390

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

Open
5 tasks done
jespertheend opened this issue Jun 4, 2022 · 6 comments
Open
5 tasks done

Deeper inference of Promise types. #49390

jespertheend opened this issue Jun 4, 2022 · 6 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@jespertheend
Copy link
Contributor

Suggestion

πŸ” Search Terms

promise required infer label:Suggestion

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

In TypeScript 4.1 the first argument of a promise's resolve function was made to be required rather than optional (see this blogpost). This is useful because it often catches legitimate bugs. The downside is that you always have to provide a type argument. It would be nice if TypeScript were better at inferring the types of Promises so that you don't always have to provide a type.
So for instance

new Promise(r => r(3));

Would automatically result in the type Promise<number>.

πŸ“ƒ Motivating Example

In TypeScript having to provide a type is not such a big deal. You can usually get away with something like:

await new Promise<number>(r => r(3));

But in Javascript with JSDoc, this same piece of code becomes:

/** @type {Promise<number>} */
const promise = new Promise(r => r(3);
await promise;

which is generally a lot less clean and less readable than the TypeScript equivalent.

πŸ’» Use Cases

I frequently find myself running across code like the following:

/** @type {(() => void)[]} */
const cbs = [];

async function test() {
    // Do something

    // wait until `cbs` have fired
    await new Promise(r => r());

    // Do something else
}

All I want to do here is wait in the middle of an async function until a certain event has happened and then move on. I won't even be using the return value of the promise most of the time.

But because the first argument is required, TypeScript will now complain that I'm calling r() without any arguments. (here's a playground example)

If the return type of the promise would be inferred, there' wouldn't be the need to create an extra variable and annotate the promise with the Promise<void> type.

@DarkGuy10
Copy link

Came across this while creating a promise that just needs to resolve on an event (don't need a value)

	/**
	 * Wait for the client to get ready.
	 */
	async getReady(): Promise<void> {
		const promise = new Promise<void>(resolve => {
			this.once('ready', () => {
				resolve()
			})
		})
		return promise
	}

Would save trouble if resolve types were automatically inferred

@fatcerberus
Copy link

fatcerberus commented Jun 4, 2022

@DarkGuy10 While this doesn't invalidate the feature request, it's worth noting that in cases like yours you can take advantage of contextual typing:

	/**
	 * Wait for the client to get ready.
	 */
	async getReady(): Promise<void> {
		return new Promise(resolve => {
			this.once('ready', () => {
				resolve()
			})
		})
	}

Assigning a new Promise() to an existing Promise type automatically infers the correct type for resolve.

P.S. this method probably doesn't need to be marked async since it returns a promise directly and never uses await. You generally want to avoid that because it imposes an extra layer of promise-wrapping and by extension, extra event loop ticks.

@DarkGuy10
Copy link

@fatcerberus thanks for the tips!

And yes, I realize now that the async is promisifying a promise. Dont know why I did that but I'll fix it!

@fatcerberus
Copy link

This issue seems related: #31146

@RyanCavanaugh
Copy link
Member

Previously attempted at #40466

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jun 6, 2022
@fatcerberus
Copy link

Previously attempted at #40466

I knew there was already an issue for this, but all my searches were unsuccessful. Glad someone else found it πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants