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

Better typings for promise.race based on mapped tuple types #27192

Closed
wants to merge 0 commits into from

Conversation

dubzzz
Copy link

@dubzzz dubzzz commented Sep 18, 2018

Following the feature - Mapped tuple types #25947 - it is now possible to have better typings for Promise.race.

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Sep 18, 2018
@dubzzz dubzzz closed this Sep 19, 2018
@dubzzz
Copy link
Author

dubzzz commented Sep 19, 2018

Actually I closed the issue because I thought that race<T>(values: (T | PromiseLike<T>)[]): Promise<T> might already handle such infinite number of parameters but my try on the playground show the contrary so I re-open the PR.

For the moment, the following code is invalid:

const waitingFirst: Promise<number|string> = Promise.race([
    Promise.resolve(1),
    Promise.resolve(1),
    Promise.resolve(1),
    Promise.resolve(1),
    Promise.resolve(1),
    Promise.resolve("e"),
    Promise.resolve(1),
    Promise.resolve(1),
    Promise.resolve(1),
    Promise.resolve("e"),
    Promise.resolve(1)
]);

But it should be fixed by this PR.

@dubzzz dubzzz reopened this Sep 19, 2018
@rbuckton
Copy link
Member

Wouldn't this be cleaner?

interface PromiseConstructor {
    // array
    race<T>(values: T[]): Promise<T extends PromiseLike<infer U> ? U : T>;
    // iterable
    race<T>(values: Iterable<T>): Promise<T extends PromiseLike<infer U> ? U : T>;
}

@dubzzz
Copy link
Author

dubzzz commented Sep 19, 2018

@rbuckton Indeed it would be cleaner. I am updating the commit accordingly

@Jessidhia
Copy link

Isn't array already Iterable? 🤔

@rbuckton
Copy link
Member

@Kovensky: It is, but we split the definition between different lib files based on how much of ES2015 your host supports.

@dubzzz
Copy link
Author

dubzzz commented Nov 9, 2018

@rbuckton Do I need to adapt something in this PR?

@rbuckton
Copy link
Member

rbuckton commented Feb 6, 2019

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 6, 2019

Heya @rbuckton, I've started to run the extended test suite on this PR at f6f4f5d. You can monitor the build here. It should now contribute to this PR's status checks.

@rbuckton
Copy link
Member

rbuckton commented Feb 7, 2019

This change may be causing a compiler crash in our RWC suite. We are investigating.

@dubzzz
Copy link
Author

dubzzz commented Feb 7, 2019

Please let me know when you get more details about the failure ;)

@rbuckton
Copy link
Member

Unfortunately, conditional types tend to result in higher memory usage in the compiler. One of our RWC test suites is crashing due to running out of memory as a result of this change. I will continue to investigate, but unfortunately will be out of the office next week. If this is something I am unable to address before Friday, I will continue to investigate when I return to the office.

@RyanCavanaugh
Copy link
Member

@dubzzz in an attempt to get this branch rebased with master, I broke everything (long story). Opened a new PR at #31117

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

Successfully merging this pull request may close these issues.

None yet

6 participants