ProgramGuru

Typing Promises

Typing Promises — the core idea

Promise<T> tells TypeScript what a successful asynchronous computation resolves to. A Promise<string> can reject for many reasons, but TypeScript does not encode the rejection type in Promise<T>; catch values should be handled carefully.

What you’ll learn

  • Explain typing promises with a concrete use case.
  • Run the example and account for every line of its output.
  • Recognize the difference between compile-time checking and runtime behavior.

Understand the rules and when to use them

Reading the code: loadScore returns Promise<number>. Await extracts its fulfilled number in main. main itself returns Promise<void>, because an async function always produces a Promise.

Keep the boundary clear: Typing Promise<number> does not guarantee the operation will succeed; rejection is still possible.

See the idea as a flow

Typing Promises — conceptual flowPromise<number> then await then numberTyping Promises1Promise<number>2await3numberAsynchronous operations may resume after the current JavaScript stack.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as typing-promises.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.

TypeScript code

function loadScore(): Promise<number> { return Promise.resolve(88); }
async function main(): Promise<void> {
  const score: number = await loadScore();
  console.log(`score=${score}`);
}
main().catch(console.error);

Verified output

score=88

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable typing-promises.ts; run: node typing-promises.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.

Trace the example line by line

loadScore returns Promise<number>. Await extracts its fulfilled number in main. main itself returns Promise<void>, because an async function always produces a Promise.

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Avoid this common mistake

Typing Promise<number> does not guarantee the operation will succeed; rejection is still possible.

Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.

Practice independently

Change the function to Promise<{ score:number }> and update the consumer to read score.

Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.

Check your understanding

What is the type of `await loadScore()` here?
Choose one answer. You can try again.

Answer and explanation

number, because await unwraps Promise<number>.

loadScore returns Promise<number>. Await extracts its fulfilled number in main. main itself returns Promise<void>, because an async function always produces a Promise.

What to remember

Promise<T> tells TypeScript what a successful asynchronous computation resolves to.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: async Functions →