0
0
Typescriptprogramming~10 mins

How TypeScript infers types automatically - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How TypeScript infers types automatically
Declare variable with value
TypeScript checks value type
Assign inferred type to variable
Use variable with inferred type
Type errors if used incorrectly
End
TypeScript looks at the value assigned to a variable and automatically decides its type, then uses that type to check for errors later.
Execution Sample
Typescript
let age = 25;
let name = "Alice";
let isStudent = true;

age = 30; // OK
// age = "thirty"; // Error
This code shows TypeScript automatically deciding the types of variables from their initial values and then checking for type errors.
Execution Table
StepCode LineActionInferred TypeType Check Result
1let age = 25;Declare 'age' with value 25numberNo error
2let name = "Alice";Declare 'name' with value "Alice"stringNo error
3let isStudent = true;Declare 'isStudent' with value truebooleanNo error
4age = 30;Assign 30 to 'age'numberNo error
5// age = "thirty";Attempt to assign string to 'age'numberError: Type 'string' not assignable to type 'number'
6EndNo more code--
💡 Execution stops because the code ends; TypeScript reports error if types mismatch.
Variable Tracker
VariableStartAfter Step 4Final
age25 (number)30 (number)30 (number)
name"Alice" (string)"Alice" (string)"Alice" (string)
isStudenttrue (boolean)true (boolean)true (boolean)
Key Moments - 2 Insights
Why does TypeScript give an error when assigning a string to 'age'?
Because at step 1, TypeScript inferred 'age' as a number, so at step 5, assigning a string causes a type mismatch error.
Does TypeScript need explicit type annotations to know variable types?
No, as shown in steps 1-3, TypeScript infers types automatically from the assigned values without explicit annotations.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2. What type does TypeScript infer for 'name'?
Anumber
Bboolean
Cstring
Dany
💡 Hint
Check the 'Inferred Type' column at step 2 in the execution table.
At which step does TypeScript detect a type error?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Type Check Result' column for errors in the execution table.
If we changed 'let age = 25;' to 'let age;', what would happen to the inferred type?
ATypeScript infers 'any' type
BTypeScript infers 'string'
CTypeScript infers 'number' anyway
DTypeScript gives an error immediately
💡 Hint
Variables without initial values get 'any' type by default; check variable initialization in the concept flow.
Concept Snapshot
TypeScript infers variable types from their initial values.
No need to write types explicitly.
Inferred types help catch errors early.
Assigning wrong types causes errors.
Type inference works for numbers, strings, booleans, and more.
Full Transcript
This example shows how TypeScript automatically figures out the type of a variable when you give it a value. For example, when you write 'let age = 25;', TypeScript knows 'age' is a number. Later, if you try to put a string in 'age', TypeScript will warn you. This helps catch mistakes early. Variables like 'name' and 'isStudent' also get their types from their first values. If you don't give a value when declaring, TypeScript treats the type as 'any', meaning it won't check types strictly. This automatic type guessing is called type inference and makes coding easier and safer.