Challenge - 5 Problems
TypeScript Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after adding types?
Consider this JavaScript function migrated to TypeScript with types added. What will be the output when calling
greet('Alice')?Typescript
function greet(name: string): string { return `Hello, ${name}!`; } console.log(greet('Alice'));
Attempts:
2 left
💡 Hint
Check how the function uses the typed parameter and returns a string.
✗ Incorrect
The function greet takes a string parameter and returns a greeting string. The output is the greeting with the name inserted.
❓ Predict Output
intermediate2:00remaining
What error occurs with missing type annotations?
Given this TypeScript code, what error will the compiler show?
Typescript
function add(a, b) { return a + b; } const result: number = add(5, '10');
Attempts:
2 left
💡 Hint
Look at the types of arguments passed and the expected return type.
✗ Incorrect
The function parameters have no types, so TypeScript infers them as any. But assigning the result to a number type causes an error because 'b' is a string.
🔧 Debug
advanced2:30remaining
Why does this TypeScript code cause a compilation error?
This JavaScript code was migrated to TypeScript but now causes an error. What is the cause?
Typescript
const user = { name: 'Bob', age: 30 }; function printUser(user: { name: string; age?: number }) { console.log(user.name.toUpperCase()); console.log(user.age.toFixed(2)); } printUser(user);
Attempts:
2 left
💡 Hint
Check optional properties and method calls on possibly undefined values.
✗ Incorrect
The 'age' property is optional, so it might be undefined. Calling toFixed on undefined causes a compilation error.
🧠 Conceptual
advanced2:00remaining
Which TypeScript feature helps migrate dynamic JavaScript objects safely?
When migrating JavaScript code that uses dynamic objects, which TypeScript feature helps ensure type safety while allowing flexible object shapes?
Attempts:
2 left
💡 Hint
Think about how to type objects with unknown keys but known value types.
✗ Incorrect
Index signatures allow typing objects with unknown property names but consistent value types, useful for dynamic objects.
❓ Predict Output
expert3:00remaining
What is the output of this TypeScript code using discriminated unions?
Consider this TypeScript code using discriminated unions. What will it print?
Typescript
type Shape =
| { kind: 'circle'; radius: number }
| { kind: 'square'; sideLength: number };
function area(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
case 'square':
return shape.sideLength ** 2;
}
}
console.log(area({ kind: 'circle', radius: 3 }).toFixed(2));Attempts:
2 left
💡 Hint
Calculate the area of a circle with radius 3 and format to 2 decimals.
✗ Incorrect
The area of a circle is π * r² = 3.14159 * 9 ≈ 28.27. The code returns this value and formats it to 2 decimals.