A promise's fulfillment type and its rejection behavior are different contracts. Built-in Awaited<T> describes the value obtained by awaiting T. ReturnType<F> describes what a function returns. Combining them lets us name a loader's fulfilled data without repeating its annotation.
Use Awaited<ReturnType<typeof load>> on a known function, or constrain a helper to zero-argument functions when that is the API you actually support. Do not use any merely to make a type extractor accept every possible function shape. A small precise contract is easier to check.
A transport returning Promise<unknown> still needs decoding after await. Awaited cannot turn that unknown into a trusted event. Nor does a Promise<Result<T>> promise that the function never rejects: a thrown programming error can still reject it. Catch only the failures you deliberately convert.
The final event gateway dispatches synchronously. An optional async transport adapter could feed it validated messages, but async listener coordination would need a different API. Keep these lifecycles distinct rather than assuming a Promise annotation solves them all.
Predict, check, and run
Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.
async function load() {
return { title: "Types", pages: 120 };
}
type Loaded = Awaited<ReturnType<typeof load>>;
const book: Loaded = await load();
console.log(book.title, book.pages);Enable JavaScript to edit and type-check this example.
Check your prediction
Types 120Read the compiler error
Predict the rejected relationship before checking. Read the real diagnostic and its source location; fix the contract or the value instead of hiding it with an assertion.
async function loadCount(): Promise<number> {
return 0;
}
type Count = Awaited<ReturnType<typeof loadCount>>;
const count: Count = loadCount();
console.log(count);Enable JavaScript to edit and type-check this example.
Check your diagnosis
The key line is TS2322: Type 'Promise<number>' is not assignable to type 'number'. The supplied type still has its Promise wrapper; the required type is the awaited value.
Count is the awaited number, but calling an async function produces a promise. Await the call before assigning.
Awaited describes fulfillment rather than rejection policy
ReturnType derives what a function returns; Awaited derives what awaiting that return makes available. Their composition is useful for a known loader, but it cannot turn unknown transport data into validated domain data.
Promise<Result<T>> also does not guarantee the function cannot reject. An expected failure may fulfill as a Result branch, while a programming defect can still throw. Test these paths separately instead of relying on one annotation to encode every failure policy.
Why use the built-in Awaited instead of writing T extends Promise<infer V> ? V : T yourself? Because awaiting is recursive: at runtime, awaiting a promise that resolves to another promise waits for both. A one-level extractor stops after the first layer.
type UnwrapOnce<T> = T extends Promise<infer V> ? V : T;
type Nested = Promise<Promise<number>>;
const once: UnwrapOnce<Nested> = Promise.resolve(25);
const fully: Awaited<Nested> = 25;
console.log(await once, fully);Enable JavaScript to edit and type-check this example.
Check your prediction
25 25UnwrapOnce<Nested> is still Promise<number>, so once must hold a promise and needs its own await; assigning 25 to it fails with TS2322 Type 'number' is not assignable to type 'Promise<number>'. Awaited<Nested> is number, which matches what await really produces. Awaited also unwraps other "thenables" (objects with a then method), as await does.
type Result<T> =
| {
ok: true;
value: T;
}
| {
ok: false;
message: string;
};
async function load(): Promise<Result<number>> {
throw new Error("defect");
}
try {
await load();
} catch (error) {
console.log(error instanceof Error ? error.message : "unknown");
}Enable JavaScript to edit and type-check this example.
Check your prediction
defectThe function still has a declared fulfillment type, even though this implementation rejects. A caller must understand which failures the implementation deliberately converts and which it propagates.
Try the rule on new inputs
Write one fulfilled Result failure and one rejected promise. Identify which is handled by narrowing and which by catch. Then derive a Promise<unknown> loader’s result and explain why a decoder is still needed.
Trace the contract before building
Separate the type returned by a call from the value obtained by awaiting it. Awaited models unwrapping at the type level; it does not start the operation or wait at runtime. Follow a concrete promise-producing function through its return type and then through the consumer's await.
Failures still need a policy. A rejection reason is not restricted by the fulfillment type, and a fulfilled Result can report an expected failure without rejecting. Test these outcomes separately and avoid conflating them with an empty success value. A precise helper should make the consumer's handling clearer while leaving the actual asynchronous sequencing visible in executable code.
Build it yourself
Awaited & Async Boundaries: practice
Write a reusable Loaded<F> that gives the value any loader function produces once awaited. It must work for loadBooks(shelf: string): Promise<readonly Book[]>, which takes an argument, and for a synchronous loadDefault() that returns a Book directly. Derive Item, one element of a loaded shelf. Use the types to print the first book's title, or the default's title when the shelf is empty: 1 A Types for shelf A and 0 Default for an empty list. Loaded<"text"> must be a compiler error.
type Book = { title: string; pages: number };
async function loadBooks(shelf: string): Promise<readonly Book[]> {
return [{ title: `${shelf} Types`, pages: 120 }];
}
function loadDefault() {
return { title: "Default", pages: 0 };
}
// Derive Loaded<F>, then the shelf, item and fallback types.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
Combine ReturnType and Awaited in one generic alias. Awaited leaves a non-promise type unchanged, so the same alias covers both loaders. Constrain F so that only functions are accepted, whatever their parameters.
Hint 2: a first step
Start with type Loaded<F extends (...args: never[]) => unknown> = .... One element of an array type is an indexed access with [number].
Show one solution
type Book = { title: string; pages: number };
async function loadBooks(shelf: string): Promise<readonly Book[]> {
return [{ title: `${shelf} Types`, pages: 120 }];
}
function loadDefault() {
return { title: "Default", pages: 0 };
}
type Loaded<F extends (...args: never[]) => unknown> = Awaited<ReturnType<F>>;
type Shelf = Loaded<typeof loadBooks>;
type Item = Shelf[number];
type Fallback = Loaded<typeof loadDefault>;
const books: Shelf = await loadBooks("A");
const first: Item | Fallback = books[0] ?? loadDefault();
const empty: Shelf = [];
const none: Item | Fallback = empty[0] ?? loadDefault();
console.log(books.length, first.title);
console.log(empty.length, none.title);Enable JavaScript to edit and type-check this example.
1 A Types
0 Default(...args: never[]) => unknown accepts every function, including loadBooks with its string parameter: a function that needs a string can be called with arguments of type never, so its parameters are not a reason to reject it. A plain string is not a function, so Loaded<"text"> gives TS2344. Shelf is readonly Book[], and with noUncheckedIndexedAccess books[0] is Book | undefined, so ?? loadDefault() provides the fallback. Like the lesson's alias, Loaded says nothing about why loadBooks might reject.
Change it and test it
Change loadBooks to throw an Error and observe that Loaded<typeof loadBooks> does not change. Then make a transport Promise<unknown> and explain why a decoder is still required.
Completion is saved in this browser only. It does not measure mastery.