Bird
Raised Fist0
Typescriptprogramming~10 mins

Explicit type annotations in Typescript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to explicitly declare the type of the variable age as number.

Typescript
let age: [1] = 25;
Drag options to blanks, or click blank then click option'
Aany
Bstring
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type for a numeric value.
Omitting the type annotation.
2fill in blank
medium

Complete the code to explicitly declare the type of the function parameter name as string.

Typescript
function greet(name: [1]) {
  return `Hello, ${name}!`;
}
Drag options to blanks, or click blank then click option'
Anumber
Bboolean
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using number type for a text parameter.
Not specifying any type.
3fill in blank
hard

Fix the error by explicitly typing the variable isActive as a boolean.

Typescript
let isActive: [1] = true;
Drag options to blanks, or click blank then click option'
Aany
Bboolean
Cstring
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Typing isActive as string or number.
Leaving out the type annotation.
4fill in blank
hard

Fill both blanks to explicitly type the array scores as an array of numbers and the variable total as a number.

Typescript
let scores: [1] = [10, 20, 30];
let total: [2] = 60;
Drag options to blanks, or click blank then click option'
Anumber[]
Bstring[]
Cnumber
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Typing scores as string[].
Typing total as string or any.
5fill in blank
hard

Fill all three blanks to explicitly type the object person with name as string, age as number, and isStudent as boolean.

Typescript
let person: { name: [1]; age: [2]; isStudent: [3] } = {
  name: "Alice",
  age: 22,
  isStudent: false
};
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types for properties.
Using any instead of specific types.

Practice

(1/5)
1. What is the main purpose of using explicit type annotations in TypeScript?
easy
A. To tell TypeScript exactly what type a variable or function uses
B. To make the code run faster
C. To automatically convert types at runtime
D. To remove all type checking

Solution

  1. Step 1: Understand what explicit type annotations do

    Explicit type annotations specify the exact type for variables or functions in TypeScript.
  2. Step 2: Identify the purpose of explicit typing

    This helps TypeScript catch mistakes and makes the code easier to understand.
  3. Final Answer:

    To tell TypeScript exactly what type a variable or function uses -> Option A
  4. Quick Check:

    Explicit type annotations = specify exact types [OK]
Hint: Explicit types tell TS variable types clearly [OK]
Common Mistakes:
  • Thinking explicit types speed up runtime
  • Confusing type annotations with type conversion
  • Believing explicit types disable type checking
2. Which of the following is the correct syntax for explicitly typing a variable as a string in TypeScript?
easy
A. let name = string;
B. let name: string;
C. let name = "string";
D. let name: "string";

Solution

  1. Step 1: Recall TypeScript variable type annotation syntax

    Type annotations use a colon after the variable name followed by the type.
  2. Step 2: Match the correct syntax

    let name: string; uses let name: string; which is the correct way to declare a string type.
  3. Final Answer:

    let name: string; -> Option B
  4. Quick Check:

    Variable: type syntax = correct [OK]
Hint: Use colon after variable name for type [OK]
Common Mistakes:
  • Using equals sign instead of colon for type
  • Putting type inside quotes incorrectly
  • Assigning type as a value instead of annotation
3. What will be the output of this TypeScript code?
let age: number = 30;
age = "thirty";
console.log(age);
medium
A. Type error at compile time
B. thirty
C. 30
D. undefined

Solution

  1. Step 1: Check the variable type and assignment

    The variable age is explicitly typed as number, but assigned a string "thirty" later.
  2. Step 2: Understand TypeScript's type checking

    TypeScript will raise a compile-time error because the string assignment violates the number type annotation.
  3. Final Answer:

    Type error at compile time -> Option A
  4. Quick Check:

    Assigning wrong type = compile error [OK]
Hint: Assign only matching types to annotated variables [OK]
Common Mistakes:
  • Assuming code runs ignoring type mismatch
  • Thinking TypeScript converts types automatically
  • Expecting runtime error instead of compile error
4. Identify the error in this TypeScript code snippet:
function greet(name: string): void {
  console.log("Hello, " + name);
}
greet(42);
medium
A. Variable name is not declared
B. Missing return type annotation
C. Function body syntax error
D. Calling function with wrong argument type

Solution

  1. Step 1: Check function parameter type

    The function greet expects a string parameter named name.
  2. Step 2: Check the function call argument

    The call greet(42) passes a number instead of a string, causing a type mismatch error.
  3. Final Answer:

    Calling function with wrong argument type -> Option D
  4. Quick Check:

    Argument type must match parameter type [OK]
Hint: Match argument types to parameter annotations [OK]
Common Mistakes:
  • Ignoring parameter type annotations
  • Assuming implicit type conversion
  • Missing function return type is not an error here
5. You want to create a function that takes an array of numbers and returns an object mapping each number to its square. Which explicit type annotation correctly describes the return type?
function squareMap(nums: number[]): ??? {
  const result = {};
  for (const num of nums) {
    result[num] = num * num;
  }
  return result;
}
hard
A. Map
B. { [key: string]: number }
C. Record
D. Array<number>

Solution

  1. Step 1: Understand the function's return structure

    The function returns an object mapping numbers to their squares, so keys and values are numbers.
  2. Step 2: Choose the best explicit return type

    TypeScript's Record<number, number> type explicitly defines an object with number keys and number values, matching the function's output.
  3. Final Answer:

    Record<number, number> -> Option C
  4. Quick Check:

    Use Record for typed object maps [OK]
Hint: Use Record for typed objects [OK]
Common Mistakes:
  • Using string keys when keys are numbers
  • Returning array instead of object
  • Using index signature with wrong key type
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