Bird
Raised Fist0
Typescriptprogramming~10 mins

How TypeScript infers types automatically - Visual Walkthrough

Choose your learning style10 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.

Practice

(1/5)
1. What type does TypeScript automatically assign to the variable declared as let count = 10;?
easy
A. number
B. string
C. any
D. boolean

Solution

  1. Step 1: Look at the assigned value

    The variable count is assigned the value 10, which is a number.
  2. Step 2: Understand TypeScript inference

    TypeScript infers the type from the assigned value, so count is inferred as number.
  3. Final Answer:

    number -> Option A
  4. Quick Check:

    Value 10 means type number [OK]
Hint: Check the assigned value type to guess variable type [OK]
Common Mistakes:
  • Thinking unassigned variables get a type
  • Confusing number with string
  • Assuming type any by default
2. Which of the following is the correct way to declare a variable with automatic type inference in TypeScript?
easy
A. let name: string = 'Alice';
B. let score: any = 100;
C. let isActive: boolean;
D. let age = 25;

Solution

  1. Step 1: Identify automatic type inference

    TypeScript infers types when you assign a value without explicitly declaring the type.
  2. Step 2: Check each option

    let age = 25; declares age with an assigned value 25 and no explicit type, so TypeScript infers number.
  3. Final Answer:

    let age = 25; -> Option D
  4. Quick Check:

    Assigned value without explicit type means inference [OK]
Hint: Look for variables declared with value but no type [OK]
Common Mistakes:
  • Confusing explicit type with inferred type
  • Choosing variables declared without assignment
  • Assuming any type is inferred by default
3. What will be the inferred type and output of this code?
let message = 'Hello';
message = 123;
medium
A. TypeScript error: cannot assign number to string
B. message becomes 123 and type is number
C. message becomes '123' as string
D. No error, message is any type

Solution

  1. Step 1: Identify inferred type from initial assignment

    Variable message is assigned a string 'Hello', so TypeScript infers type string.
  2. Step 2: Check reassignment compatibility

    Assigning a number 123 to a variable inferred as string causes a type error.
  3. Final Answer:

    TypeScript error: cannot assign number to string -> Option A
  4. Quick Check:

    Inferred string type rejects number assignment [OK]
Hint: Inferred type fixes variable type, no mixed types allowed [OK]
Common Mistakes:
  • Assuming variable type changes on reassignment
  • Thinking TypeScript converts types automatically
  • Ignoring type errors on incompatible assignments
4. Find the error in this TypeScript code:
let data = [1, 2, 3];
data.push('four');
medium
A. No error, array can hold any type
B. Error: cannot push string into number array
C. Error: push method does not exist
D. Error: data is not initialized

Solution

  1. Step 1: Identify inferred array type

    Variable data is assigned an array of numbers, so TypeScript infers type number[].
  2. Step 2: Check push argument type

    Trying to push a string 'four' into a number[] array causes a type error.
  3. Final Answer:

    Error: cannot push string into number array -> Option B
  4. Quick Check:

    Array type enforces element types [OK]
Hint: Array inferred type fixes allowed element types [OK]
Common Mistakes:
  • Assuming arrays accept mixed types by default
  • Thinking push method is unavailable
  • Ignoring type errors on array element types
5. Given this code, what is the inferred type of result and why?
const numbers = [1, 2, 3];
const result = numbers.map(n => n * 2);
hard
A. void, because map does not return anything
B. any[], because map returns any type by default
C. number[], because map returns an array of numbers
D. number, because map returns a single number

Solution

  1. Step 1: Identify input array type

    numbers is an array of numbers, so type is number[].
  2. 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 returns n * 2 (a number), the result is number[].
  3. Final Answer:

    number[], because map returns an array of numbers -> Option C
  4. Quick Check:

    map returns array of transformed elements [OK]
Hint: map returns array with same length and inferred element type [OK]
Common Mistakes:
  • Thinking map returns a single value
  • Assuming map returns any[] by default
  • Confusing void return with map's return
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard