Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created January 29, 2024 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmiller1990/067b9d296bf5a3b05151437009ad7235 to your computer and use it in GitHub Desktop.
Save lmiller1990/067b9d296bf5a3b05151437009ad7235 to your computer and use it in GitHub Desktop.
awaited.ts
type Awaited<T> = T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F, ...args: infer _): any; } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: infer _) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment