Skip to main content
D
TypeScript
TypeScript Type Inference

TypeScript Type Inference

TypeScript guesses the correct data type automatically.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

You don't always have to write out the data types manually. If you assign a value to a variable immediately, TypeScript acts like a detective and figures out the correct type behind the scenes.

Example

Example
1/* this uses inference */
2let greeting = "Hello World"; // TypeScript knows this is a string
3
4// greeting = 42; // This causes a massive error!

Key Points

  • "Inference" means guessing automatically.
  • It saves time and keeps the code clean.
  • It still locks the type permanently after guessing.
  • You only explicitly write types when you don't assign an initial value.

Up Next

Continue your journey with the next topic.

Go to TS Interfaces