0
0
Typescriptprogramming~20 mins

How TypeScript infers types automatically - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Type Inference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
Anumber
Bany
Cstring
Dunknown
Attempts:
2 left
💡 Hint
TypeScript looks at the value assigned to infer the type.
Predict Output
intermediate
2: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}!`;
}
Aany
Bunknown
Cstring
Dnumber
Attempts:
2 left
💡 Hint
Default value helps TypeScript guess the parameter type.
Predict Output
advanced
2: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"];
Astring[]
Bany[]
CArray<number>
Dunknown[]
Attempts:
2 left
💡 Hint
All elements are strings, so TypeScript infers an array of strings.
Predict Output
advanced
2: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;
Avoid
Bnumber
Cany
Dstring
Attempts:
2 left
💡 Hint
The function returns the product of two numbers.
Predict Output
expert
2: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());
Aany[]
Bnumber[]
Cstring[]
D(string | number)[]
Attempts:
2 left
💡 Hint
The map returns either a number or a string depending on the item type.