Bird
Raised Fist0
Typescriptprogramming~20 mins

Migrating JavaScript to TypeScript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of migrating JavaScript code to TypeScript?
easy
A. It adds type safety to catch errors early
B. It makes the code run faster in browsers
C. It removes all JavaScript features
D. It automatically converts code to Python

Solution

  1. Step 1: Understand TypeScript's purpose

    TypeScript adds static types to JavaScript, helping find errors before running code.
  2. Step 2: Compare options

    Only It adds type safety to catch errors early correctly states the benefit of type safety. Other options are incorrect or unrelated.
  3. Final Answer:

    It adds type safety to catch errors early -> Option A
  4. Quick Check:

    Type safety = D [OK]
Hint: TypeScript adds types to catch errors early [OK]
Common Mistakes:
  • Thinking TypeScript makes code run faster
  • Believing TypeScript removes JavaScript features
  • Confusing TypeScript with other languages
2. Which of the following is the correct way to declare a typed variable in TypeScript?
easy
A. let age = number 30;
B. let age: number = 30;
C. let age = 30: number;
D. let age number = 30;

Solution

  1. Step 1: Recall TypeScript variable syntax

    TypeScript uses let variableName: type = value; to declare typed variables.
  2. Step 2: Check each option

    Only let age: number = 30; follows the correct syntax. Others have misplaced type or incorrect order.
  3. Final Answer:

    let age: number = 30; -> Option B
  4. Quick Check:

    Correct typed variable syntax = B [OK]
Hint: Use colon after variable name for type [OK]
Common Mistakes:
  • Placing type after value
  • Omitting colon between name and type
  • Using wrong order of type and value
3. What will be the output of this TypeScript code after migration?
function greet(name: string) {
  return `Hello, ${name.toUpperCase()}!`;
}
console.log(greet("world"));
medium
A. TypeError at runtime
B. "Hello, world!"
C. "Hello, WORLD!"
D. SyntaxError during compilation

Solution

  1. Step 1: Analyze the function behavior

    The function takes a string and returns a greeting with the name in uppercase.
  2. Step 2: Evaluate the output of greet("world")

    Calling greet("world") returns "Hello, WORLD!" because toUpperCase() converts to uppercase.
  3. Final Answer:

    "Hello, WORLD!" -> Option C
  4. Quick Check:

    String uppercase greeting = C [OK]
Hint: Check string methods and template literals output [OK]
Common Mistakes:
  • Ignoring toUpperCase effect
  • Expecting original case output
  • Confusing runtime and compile errors
4. Identify the error in this TypeScript migration:
function add(a, b: number): number {
  return a + b;
}
medium
A. Parameter 'a' is missing a type annotation
B. Return type 'number' is invalid
C. Function name 'add' is not allowed
D. Parameters must not have types in TypeScript

Solution

  1. Step 1: Check parameter types

    In TypeScript, all parameters should have type annotations for clarity and safety.
  2. Step 2: Identify missing type

    Parameter 'a' lacks a type, causing a compile error. Other parts are correct.
  3. Final Answer:

    Parameter 'a' is missing a type annotation -> Option A
  4. Quick Check:

    All parameters need types = A [OK]
Hint: All function parameters need types in TypeScript [OK]
Common Mistakes:
  • Leaving some parameters untyped
  • Misunderstanding return type syntax
  • Thinking function names have restrictions
5. You have this JavaScript object:
const user = { id: 1, name: "Alice", active: true };
How would you migrate it to TypeScript with a proper type annotation?
hard
A. const user: any = { id: 1, name: "Alice", active: true };
B. const user = { id: number, name: string, active: boolean } = { id: 1, name: "Alice", active: true };
C. const user: object = { id: 1, name: "Alice", active: true };
D. const user: { id: number; name: string; active: boolean } = { id: 1, name: "Alice", active: true };

Solution

  1. Step 1: Define precise object type

    Use an inline type annotation with exact property types for clarity and safety.
  2. Step 2: Compare options

    const user: { id: number; name: string; active: boolean } = { id: 1, name: "Alice", active: true }; correctly annotates the object with property types. const user = { id: number, name: string, active: boolean } = { id: 1, name: "Alice", active: true }; is invalid syntax. const user: object = { id: 1, name: "Alice", active: true }; is too generic. const user: any = { id: 1, name: "Alice", active: true }; disables type checking.
  3. Final Answer:

    const user: { id: number; name: string; active: boolean } = { id: 1, name: "Alice", active: true }; -> Option D
  4. Quick Check:

    Precise object type annotation = A [OK]
Hint: Use inline type with property names and types [OK]
Common Mistakes:
  • Using 'any' which disables type safety
  • Incorrect syntax for object type annotation
  • Using generic 'object' type losing property info
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard