Created
January 29, 2024 10:58
-
-
Save lmiller1990/067b9d296bf5a3b05151437009ad7235 to your computer and use it in GitHub Desktop.
awaited.ts
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
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