0
0
Typescriptprogramming~20 mins

Migrating JavaScript to TypeScript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'));
ATypeError at runtime
BHello, undefined!
CHello, Alice!
DCompilation error due to missing return type
Attempts:
2 left
💡 Hint
Check how the function uses the typed parameter and returns a string.
Predict Output
intermediate
2: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');
AType 'string' is not assignable to type 'number'
BNo error, result is 15
CType 'number' is not assignable to type 'string'
DRuntime error: cannot add number and string
Attempts:
2 left
💡 Hint
Look at the types of arguments passed and the expected return type.
🔧 Debug
advanced
2: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);
AFunction printUser has incorrect parameter type
BProperty 'name' is missing a type annotation
CObject 'user' is missing the 'age' property
DProperty 'age' might be undefined, so calling toFixed causes an error
Attempts:
2 left
💡 Hint
Check optional properties and method calls on possibly undefined values.
🧠 Conceptual
advanced
2: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?
AIndex signatures
BUnion types
CType assertions (casting)
DEnums
Attempts:
2 left
💡 Hint
Think about how to type objects with unknown keys but known value types.
Predict Output
expert
3: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));
A9
B28.27
CNaN
DCompilation error: missing return in switch
Attempts:
2 left
💡 Hint
Calculate the area of a circle with radius 3 and format to 2 decimals.