Bird
Raised Fist0
Typescriptprogramming~20 mins

Why type annotations are needed in Typescript - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
TypeScript Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code with type annotations?

Look at this TypeScript code. What will it print when run?

Typescript
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("Alice"));
ASyntaxError
BHello, Alice!
CTypeError at runtime
DHello, undefined!
Attempts:
2 left
💡 Hint

Think about what the function returns and the argument passed.

🧠 Conceptual
intermediate
1:30remaining
Why do we use type annotations in TypeScript?

Which of these is the main reason to use type annotations in TypeScript?

ATo reduce the file size of the code
BTo make the code run faster
CTo catch errors before running the code
DTo make the code look prettier
Attempts:
2 left
💡 Hint

Think about when errors are found in TypeScript compared to JavaScript.

❓ Predict Output
advanced
2:00remaining
What error does this TypeScript code raise without type annotations?

Consider this TypeScript code without type annotations. What error will it cause?

Typescript
function add(a, b) {
  return a + b;
}

console.log(add(5, "10"));
A510
B15
CTypeError at runtime
DCompilation error due to missing types
Attempts:
2 left
💡 Hint

Think about what happens when you add a number and a string in JavaScript.

🔧 Debug
advanced
2:30remaining
Identify the error caused by missing type annotations

What error will this TypeScript code produce when compiled?

Typescript
function multiply(a: number, b: number) {
  return a * b;
}

console.log(multiply(4, "3"));
ACompilation error: Argument of type 'string' is not assignable to parameter of type 'number'.
BRuntime error: Cannot multiply number and string
COutput: 12
DNo error, outputs '43'
Attempts:
2 left
💡 Hint

Check the types of arguments passed to the function.

🚀 Application
expert
3:00remaining
How do type annotations improve code maintenance?

Choose the best explanation of how type annotations help maintain large TypeScript projects.

AThey allow the program to run without any testing.
BThey automatically fix bugs in the code without developer input.
CThey speed up the program execution by optimizing machine code.
DThey make it easier to understand what data types functions expect and return, reducing bugs when changing code.
Attempts:
2 left
💡 Hint

Think about how clear type information helps developers work on code safely.

Practice

(1/5)
1. Why do we use type annotations in TypeScript?
let age: number = 25;
easy
A. To tell the computer what kind of data to expect
B. To make the program run faster
C. To hide the data from other parts of the program
D. To automatically fix errors in the code

Solution

  1. Step 1: Understand the purpose of type annotations

    Type annotations specify the kind of data a variable should hold, like number or string.
  2. Step 2: Identify the benefit in the example

    In let age: number = 25;, the annotation number tells the computer to expect a number value.
  3. Final Answer:

    To tell the computer what kind of data to expect -> Option A
  4. Quick Check:

    Type annotations = tell data type [OK]
Hint: Type annotations describe data type before running code [OK]
Common Mistakes:
  • Thinking type annotations speed up program execution
  • Believing type annotations hide data
  • Assuming type annotations fix errors automatically
2. Which of the following is the correct way to add a type annotation for a string variable in TypeScript?
easy
A. let name = string 'Alice';
B. let name == 'Alice': string;
C. let name: 'Alice' = string;
D. let name: string = 'Alice';

Solution

  1. Step 1: Recall TypeScript syntax for type annotations

    The correct syntax is let variableName: type = value;.
  2. Step 2: Match syntax with options

    let name: string = 'Alice'; matches this syntax: let name: string = 'Alice'; is correct.
  3. Final Answer:

    let name: string = 'Alice'; -> Option D
  4. Quick Check:

    Correct syntax = let name: string = 'Alice'; [OK]
Hint: Use colon after variable name for type [OK]
Common Mistakes:
  • Using '=' instead of ':' for type annotation
  • Placing type after value
  • Using double equals '==' for assignment
3. What will happen when you run this TypeScript code?
let count: number = 10;
count = 'ten';
medium
A. The code runs but count remains 10
B. The code runs and count becomes 'ten'
C. A compile-time error occurs because 'ten' is not a number
D. A runtime error occurs when assigning 'ten'

Solution

  1. Step 1: Check the declared type of count

    Variable count is declared as number.
  2. Step 2: Analyze the assignment of a string to a number

    Assigning 'ten' (a string) to count violates the type annotation.
  3. Step 3: Understand TypeScript behavior

    TypeScript will show a compile-time error before running the code.
  4. Final Answer:

    A compile-time error occurs because 'ten' is not a number -> Option C
  5. Quick Check:

    Type mismatch = compile-time error [OK]
Hint: Assign values matching declared type to avoid errors [OK]
Common Mistakes:
  • Thinking TypeScript allows wrong type assignments at runtime
  • Confusing compile-time and runtime errors
  • Assuming variable value stays unchanged silently
4. Identify the error in this TypeScript code and choose the fix:
function greet(name) {
  return 'Hello, ' + name.toUpperCase();
}
greet(123);
medium
A. Call greet with a string instead of a number
B. Add type annotation: name: string in function parameter
C. Remove the function parameter
D. Change toUpperCase() to toString()

Solution

  1. Step 1: Understand the error cause

    The function expects a string to call toUpperCase(), but 123 is a number.
  2. Step 2: Fix by adding type annotation

    Adding name: string tells TypeScript to expect a string and catch wrong calls like greet(123).
  3. Final Answer:

    Add type annotation: name: string in function parameter -> Option B
  4. Quick Check:

    Type annotation prevents wrong argument types [OK]
Hint: Add parameter types to catch wrong argument types early [OK]
Common Mistakes:
  • Ignoring type annotations and relying on runtime errors
  • Changing method instead of fixing parameter type
  • Removing parameters instead of typing them
5. You have this TypeScript function:
function calculateArea(width, height) {
  return width * height;
}

How can adding type annotations improve this function?
hard
A. By ensuring both width and height are numbers to avoid wrong calculations
B. By making the function run faster
C. By allowing width and height to be any type
D. By automatically converting strings to numbers

Solution

  1. Step 1: Identify the problem without type annotations

    Without types, width and height can be any type, causing wrong results if strings or other types are passed.
  2. Step 2: Add type annotations to improve safety

    Adding width: number and height: number ensures only numbers are accepted, preventing errors.
  3. Final Answer:

    By ensuring both width and height are numbers to avoid wrong calculations -> Option A
  4. Quick Check:

    Type annotations improve input safety [OK]
Hint: Type inputs to avoid wrong data causing bugs [OK]
Common Mistakes:
  • Thinking type annotations speed up execution
  • Assuming type annotations convert data types automatically
  • Allowing any type defeats purpose of annotations
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