Type inference is TypeScript's ability to automatically figure out the type of a variable or expression without you explicitly telling it.
Click to reveal answer
beginner
How does TypeScript infer the type of a variable declared with <code>let x = 5;</code>?
TypeScript sees the value 5 is a number, so it infers the type of x as number.
Click to reveal answer
intermediate
Can TypeScript infer types for function return values? Give an example.
Yes. For example, <code>function add(a: number, b: number) { return a + b; }</code> TypeScript infers the return type as <code>number</code> because <code>a + b</code> is a number.
Click to reveal answer
beginner
What happens if you declare a variable without initializing it, like <code>let y;</code>?
TypeScript infers the type as any because it has no information about what type y should be.
Click to reveal answer
intermediate
How does TypeScript infer types in arrays? Example with <code>const nums = [1, 2, 3];</code>
TypeScript looks at the elements and infers the array type as number[] because all elements are numbers.
Click to reveal answer
What type does TypeScript infer for let name = "Alice";?
Aboolean
Bnumber
Cany
Dstring
✗ Incorrect
TypeScript sees the value is a string, so it infers the type as string.
If you write let data; without assigning a value, what type does TypeScript infer?
Aany
Bstring
Cundefined
Dnever
✗ Incorrect
Without an initial value, TypeScript infers the type as any.
What is the inferred return type of this function?
function greet() { return "Hi"; }
Astring
Bvoid
Cany
Dnumber
✗ Incorrect
The function returns a string, so TypeScript infers the return type as string.
Given const flags = [true, false, true];, what type does TypeScript infer for flags?
Anumber[]
Bboolean[]
Cstring[]
Dany[]
✗ Incorrect
All elements are booleans, so TypeScript infers the array type as boolean[].
Which of these is NOT a benefit of type inference in TypeScript?
ALess need to write explicit types
BCatch errors early
CCode runs faster at runtime
DBetter code readability
✗ Incorrect
Type inference helps with development but does not affect runtime speed.
Explain how TypeScript infers the type of a variable when you assign a value to it.
Think about how TypeScript guesses the type from the value you give.
You got /3 concepts.
Describe what happens when you declare a variable without initializing it in TypeScript.
What does TypeScript do when it has no clues about the variable's type?
You got /3 concepts.
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
Step 1: Look at the assigned value
The variable count is assigned the value 10, which is a number.
Step 2: Understand TypeScript inference
TypeScript infers the type from the assigned value, so count is inferred as number.
Final Answer:
number -> Option A
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
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; declares age with an assigned value 25 and no explicit type, so TypeScript infers number.
Final Answer:
let age = 25; -> Option D
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
Step 1: Identify inferred type from initial assignment
Variable message is assigned a string 'Hello', so TypeScript infers type string.
Step 2: Check reassignment compatibility
Assigning a number 123 to a variable inferred as string causes a type error.
Final Answer:
TypeScript error: cannot assign number to string -> Option A
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
Step 1: Identify inferred array type
Variable data is assigned an array of numbers, so TypeScript infers type number[].
Step 2: Check push argument type
Trying to push a string 'four' into a number[] array causes a type error.
Final Answer:
Error: cannot push string into number array -> Option B
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
Step 1: Identify input array type
numbers is an array of numbers, so type is number[].
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[].
Final Answer:
number[], because map returns an array of numbers -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.