Created
June 6, 2019 15:10
-
-
Save miyaokamarina/2519fd4bdb0fbc5e46a456e1545571e9 to your computer and use it in GitHub Desktop.
Recursive Promise unwrapper for TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Works exactly as `await`/`then` in JavaScript, but for types! | |
// Of course, it can’t handle deep nesting (≥45±n levels) due to | |
// TypeScript limitations. But 45 is enough. | |
// License: MIT. | |
type Await<a> = { | |
0: Await<a extends PromiseLike<infer b> ? b : never>, | |
1: a, | |
}[a extends PromiseLike<any> ? 0 : 1]; | |
type A = Await<Promise<Promise<Promise<Promise<number>>>>>; // number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment