TypeScript guesses the type of a value without you telling it. This helps catch mistakes early and makes coding easier.
How TypeScript infers types automatically
let variable = value; function example() { return value; }
TypeScript looks at the value on the right side to decide the type.
You don't have to write the type explicitly if TypeScript can figure it out.
age is a number because 25 is a number.let age = 25;
name as a string from the text "Alice".let name = "Alice";
greet returns a string, so TypeScript infers its return type as string.function greet() { return "Hello!"; }
numbers as an array of numbers (number[]).const numbers = [1, 2, 3];
This program shows TypeScript guessing types for variables and function return values. count is a number, so double knows x is a number and returns a number.
let count = 10; let message = "You have " + count + " new messages."; function double(x: number) { return x * 2; } const result = double(count); console.log(message); console.log(result);
If you don't assign a value when declaring a variable, TypeScript will give it the type any, which means it can be anything.
You can still add explicit types if you want to be clear or catch errors sooner.
TypeScript automatically guesses types from the values you assign.
This helps find mistakes and makes your code safer without extra work.
You can mix inferred types with explicit types as needed.
Practice
let count = 10;?Solution
Step 1: Look at the assigned value
The variablecountis assigned the value10, which is a number.Step 2: Understand TypeScript inference
TypeScript infers the type from the assigned value, socountis inferred asnumber.Final Answer:
number -> Option AQuick Check:
Value 10 means type number [OK]
- Thinking unassigned variables get a type
- Confusing number with string
- Assuming type any by default
Solution
Step 1: Identify automatic type inference
TypeScript infers types when you assign a value without explicitly declaring the type.Step 2: Check each option
let age = 25; declaresagewith an assigned value25and no explicit type, so TypeScript infersnumber.Final Answer:
let age = 25; -> Option DQuick Check:
Assigned value without explicit type means inference [OK]
- Confusing explicit type with inferred type
- Choosing variables declared without assignment
- Assuming any type is inferred by default
let message = 'Hello'; message = 123;
Solution
Step 1: Identify inferred type from initial assignment
Variablemessageis assigned a string'Hello', so TypeScript infers typestring.Step 2: Check reassignment compatibility
Assigning a number123to a variable inferred asstringcauses a type error.Final Answer:
TypeScript error: cannot assign number to string -> Option AQuick Check:
Inferred string type rejects number assignment [OK]
- Assuming variable type changes on reassignment
- Thinking TypeScript converts types automatically
- Ignoring type errors on incompatible assignments
let data = [1, 2, 3];
data.push('four');Solution
Step 1: Identify inferred array type
Variabledatais assigned an array of numbers, so TypeScript infers typenumber[].Step 2: Check push argument type
Trying to push a string'four'into anumber[]array causes a type error.Final Answer:
Error: cannot push string into number array -> Option BQuick Check:
Array type enforces element types [OK]
- Assuming arrays accept mixed types by default
- Thinking push method is unavailable
- Ignoring type errors on array element types
result and why?const numbers = [1, 2, 3]; const result = numbers.map(n => n * 2);
Solution
Step 1: Identify input array type
numbersis an array of numbers, so type isnumber[].Step 2: Understand map return type
Map applies a function to each element and returns a new array of the same length with transformed elements. Since the function returnsn * 2(a number), the result isnumber[].Final Answer:
number[], because map returns an array of numbers -> Option CQuick Check:
map returns array of transformed elements [OK]
- Thinking map returns a single value
- Assuming map returns any[] by default
- Confusing void return with map's return
Ready to prove it?
Pass the test and this pattern moves unknown
