Skip to content

Instantly share code, notes, and snippets.

@miyaokamarina
Created June 6, 2019 15:10
Show Gist options
  • Save miyaokamarina/2519fd4bdb0fbc5e46a456e1545571e9 to your computer and use it in GitHub Desktop.
Save miyaokamarina/2519fd4bdb0fbc5e46a456e1545571e9 to your computer and use it in GitHub Desktop.
Recursive Promise unwrapper for TypeScript
// 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