Challenge - 5 Problems
TypeScript Type Inference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the inferred type of variable 'x'?
Consider this TypeScript code snippet. What is the inferred type of variable
x?Typescript
let x = 42;
Attempts:
2 left
💡 Hint
TypeScript looks at the value assigned to infer the type.
✗ Incorrect
Since 42 is a number, TypeScript infers the type of x as number.
❓ Predict Output
intermediate2:00remaining
What is the inferred type of the function parameter?
Look at this function. What type does TypeScript infer for the parameter
name?Typescript
function greet(name = "Guest") { return `Hello, ${name}!`; }
Attempts:
2 left
💡 Hint
Default value helps TypeScript guess the parameter type.
✗ Incorrect
The default value "Guest" is a string, so TypeScript infers the parameter type as string.
❓ Predict Output
advanced2:00remaining
What is the inferred type of the array elements?
What type does TypeScript infer for the elements of this array?
Typescript
const fruits = ["apple", "banana", "cherry"];
Attempts:
2 left
💡 Hint
All elements are strings, so TypeScript infers an array of strings.
✗ Incorrect
Since all elements are strings, TypeScript infers the type as string array (string[]).
❓ Predict Output
advanced2:00remaining
What is the inferred return type of this arrow function?
What type does TypeScript infer as the return type of this arrow function?
Typescript
const multiply = (a: number, b: number) => a * b;Attempts:
2 left
💡 Hint
The function returns the product of two numbers.
✗ Incorrect
The function returns the product of two numbers, so the return type is inferred as number.
❓ Predict Output
expert2:00remaining
What is the inferred type of the variable 'result'?
Given this code, what is the inferred type of
result?Typescript
const data = [1, "two", 3]; const result = data.map(item => typeof item === "number" ? item * 2 : item.toUpperCase());
Attempts:
2 left
💡 Hint
The map returns either a number or a string depending on the item type.
✗ Incorrect
The map function returns numbers doubled or strings uppercased, so the result is an array of strings or numbers.