0Pricing
TypeScript Academy · Lesson

Type Inference and Contextual Typing

Learn how TypeScript automatically infers types

Type Inference and Contextual Typing is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

What is Type Inference?

Type inference is a feature in TypeScript that automatically determines the type of a variable or expression based on its value.

For example, when you write let age = 25;, TypeScript infers that age is of type number.

Type Inference and Contextual Typing — illustration 1

2

Benefits of Type Inference

Type inference reduces the need to explicitly declare types, making your code cleaner and easier to write.

It also helps TypeScript catch errors during development, ensuring type safety without adding extra annotations.

3

Examples of Type Inference

Here are some examples: 

  • let message = 'Hello'; - TypeScript infers the type as string
  • const isActive = true; - TypeScript infers the type as boolean.

Type inference also works with function return values:

function add(a: number, b: number) {
  return a + b;
}

4

What is Contextual Typing?

Contextual typing occurs when TypeScript assigns a type to a variable based on its surrounding context.

For example, when using an event listener, TypeScript infers the type of the event parameter:

document.addEventListener('click', (event) => {
  console.log(event.clientX); // TypeScript knows event is a MouseEvent
});
document.addEventListener('click', (event) => {
  console.log(event.clientX);
});

5

Benefits of Contextual Typing

Contextual typing improves the developer experience by reducing the need for manual type annotations.

It helps catch errors and ensures that you are working with the correct types in specific contexts.

6

Common Scenarios for Type Inference

TypeScript performs inference in many situations, such as:

  • Variable initialization: let name = 'John';
  • Function return values: function greet() { return 'Hello'; }
  • Array values: let numbers = [1, 2, 3]; (inferred as number[]).

7

When to Use Explicit Types

While TypeScript's type inference is powerful, there are cases where explicit types are better:

  • When the type is ambiguous or complex.
  • For function parameters to ensure clarity.
  • For public APIs where explicit types improve readability and maintainability.

9

Practice: Type Inference and Contextual Typing

Write a function that takes two numbers as input and returns their sum. TypeScript will infer the types for you!

Example:

function addNumbers(a, b) {
  return a + b;
}
function addNumbers(a, b) {
  return a + b;
}

10

Great Work!

You’ve learned how TypeScript uses type inference and contextual typing to make your code efficient and type-safe. Keep exploring TypeScript’s powerful features to enhance your coding skills!

Type Inference and Contextual Typing — illustration 9

Frequently asked questions

Is the “Type Inference and Contextual Typing” lesson free?

Yes — the full text of “Type Inference and Contextual Typing” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Type Inference and Contextual Typing”?

Learn how TypeScript automatically infers types You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Type Inference and Contextual Typing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Type Inference and Contextual Typing
  2. Decorators in TypeScript
  3. TypeScript Modules and Namespaces
← Back to TypeScript Academy