Challenge - 5 Problems
TypeScript Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code with type annotations?
Consider this TypeScript code snippet. What will it output when compiled and run?
Typescript
function greet(name: string) { return `Hello, ${name.toUpperCase()}!`; } console.log(greet("world"));
Attempts:
2 left
💡 Hint
Look at how the string method toUpperCase() is used on a typed parameter.
✗ Incorrect
The function greet expects a string and calls toUpperCase() on it, which converts 'world' to 'WORLD'. The output is 'Hello, WORLD!'.
🧠 Conceptual
intermediate1:30remaining
Why does TypeScript help catch errors earlier than JavaScript?
Which of these best explains why TypeScript can catch errors before running the code?
Attempts:
2 left
💡 Hint
Think about what happens before the program runs.
✗ Incorrect
TypeScript performs static type checking during compilation, which helps find type-related errors early, unlike JavaScript which checks types only at runtime.
🔧 Debug
advanced2:00remaining
What error does this TypeScript code produce?
Look at this TypeScript code. What error will the compiler show?
Typescript
function add(a: number, b: number): number { return a + b; } const result = add(5, "10"); console.log(result);
Attempts:
2 left
💡 Hint
Check the types of arguments passed to the function.
✗ Incorrect
The function expects two numbers, but a string is passed as the second argument. TypeScript compiler flags this as a type error.
🚀 Application
advanced1:30remaining
How does TypeScript improve code maintainability in large projects?
Which option best describes how TypeScript helps maintain large codebases?
Attempts:
2 left
💡 Hint
Think about how clear types help developers.
✗ Incorrect
Explicit types in TypeScript provide clear contracts for functions and variables, making it easier to understand, refactor, and avoid bugs in large projects.
❓ Predict Output
expert2:30remaining
What is the output of this TypeScript code using interfaces and optional properties?
Analyze this TypeScript code and select the correct output.
Typescript
interface User {
name: string;
age?: number;
}
function describe(user: User) {
return `Name: ${user.name}, Age: ${user.age ?? 'unknown'}`;
}
console.log(describe({ name: 'Alice' }));Attempts:
2 left
💡 Hint
Look at how the optional age property is handled with the nullish coalescing operator.
✗ Incorrect
The age property is optional and not provided, so user.age is undefined. The nullish coalescing operator (??) replaces undefined with 'unknown'.