Bird
Raised Fist0
Typescriptprogramming~10 mins

Static members with types 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 declare a static property named count of type number.

Typescript
class Counter {
  static [1]: number = 0;
}
Drag options to blanks, or click blank then click option'
Avalue
Bcount
Ctotal
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance property syntax instead of static
Choosing wrong property name
2fill in blank
medium

Complete the code to declare a static method named getCount that returns a number.

Typescript
class Counter {
  static count: number = 0;
  static [1](): number {
    return Counter.count;
  }
}
Drag options to blanks, or click blank then click option'
AsetCount
Bcount
CcountValue
DgetCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance method syntax
Wrong method name
3fill in blank
hard

Fix the error in the static method declaration to correctly access the static property count.

Typescript
class Counter {
  static count: number = 0;
  static getCount(): number {
    return [1].count;
  }
}
Drag options to blanks, or click blank then click option'
ACounter
Binstance
Cself
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using this to access static properties
Using undefined identifiers
4fill in blank
hard

Fill both blanks to declare a static readonly property named MAX_LIMIT of type number with value 100.

Typescript
class Limit {
  [1] readonly [2]: number = 100;
}
Drag options to blanks, or click blank then click option'
Areadonly
Bstatic
CMAX_LIMIT
Dlimit
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping order of keywords
Using wrong property name
5fill in blank
hard

Fill all three blanks to declare a static method named increment that increases the static count by 1 and returns the new count.

Typescript
class Counter {
  static count: number = 0;
  static [1](): number {
    Counter.count [2]= 1;
    return [3];
  }
}
Drag options to blanks, or click blank then click option'
Aincrement
B+
CCounter.count
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operator like -=
Returning wrong variable

Practice

(1/5)
1. Which statement about static members in TypeScript is true?
easy
A. Static members are private by default.
B. Static members can only be accessed through instances of the class.
C. Static members belong to the class itself, not to instances.
D. Static members must be declared inside the constructor.

Solution

  1. Step 1: Understand what static means

    Static members belong to the class itself, not to any object created from the class.
  2. Step 2: Check each option

    Static members belong to the class itself, not to instances. correctly states this. Static members can only be accessed through instances of the class. is wrong because static members are accessed via the class, not instances. Static members are private by default. is false; static members can have any access modifier. Static members must be declared inside the constructor. is incorrect; static members are declared outside the constructor.
  3. Final Answer:

    Static members belong to the class itself, not to instances. -> Option C
  4. Quick Check:

    Static = class-level member [OK]
Hint: Remember: static means class-level, not instance-level [OK]
Common Mistakes:
  • Thinking static members belong to instances
  • Trying to access static members via objects
  • Assuming static members are private by default
2. Which of the following is the correct syntax to declare a static property with type number in a TypeScript class?
easy
A. static count: number = 0;
B. count static: number = 0;
C. static number count = 0;
D. number static count = 0;

Solution

  1. Step 1: Recall static property syntax

    In TypeScript, static properties are declared with the keyword static before the property name and type.
  2. Step 2: Check each option

    static count: number = 0; uses correct order: static count: number = 0;. Options B, C, and D have incorrect order of keywords or types.
  3. Final Answer:

    static count: number = 0; -> Option A
  4. Quick Check:

    static + name + : + type + = value [OK]
Hint: Static keyword always comes before property name [OK]
Common Mistakes:
  • Placing type before static keyword
  • Swapping order of static and property name
  • Missing colon between property name and type
3. What will be the output of the following TypeScript code?
class Counter {
  static count: number = 0;
  increment() {
    Counter.count++;
  }
}
const c1 = new Counter();
const c2 = new Counter();
c1.increment();
c2.increment();
console.log(Counter.count);
medium
A. 0
B. 1
C. undefined
D. 2

Solution

  1. Step 1: Understand static property usage

    The static property count belongs to the class Counter, shared by all instances.
  2. Step 2: Trace the increments

    Both c1.increment() and c2.increment() increase Counter.count by 1 each, so total increments = 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Static count increments twice = 2 [OK]
Hint: Static properties are shared; increments add up [OK]
Common Mistakes:
  • Thinking static count resets per instance
  • Expecting output 1 instead of 2
  • Confusing instance and static properties
4. Identify the error in this TypeScript class using static members:
class Example {
  static value: string;
  constructor() {
    this.value = "Hello";
  }
}
medium
A. Cannot assign to static property using this inside constructor.
B. Static properties must be initialized outside the class.
C. Static properties cannot have type annotations.
D. Constructor cannot be used with static properties.

Solution

  1. Step 1: Understand static property access

    Static properties belong to the class, so they must be accessed via the class name, not this.
  2. Step 2: Identify the error in constructor

    Using this.value tries to access an instance property, but value is static. Correct access is Example.value.
  3. Final Answer:

    Cannot assign to static property using this inside constructor. -> Option A
  4. Quick Check:

    Static accessed via class, not this [OK]
Hint: Use ClassName, not this, to access static members [OK]
Common Mistakes:
  • Using this to access static properties
  • Thinking static properties must be initialized outside class
  • Believing constructor can't coexist with static members
5. Given this TypeScript class, how can you add a static method reset that sets count back to zero?
class Tracker {
  static count: number = 0;
  static increment() {
    this.count++;
  }
}
hard
A. Add reset() { Tracker.count = 0; } inside the class.
B. Add static reset() { this.count = 0; } inside the class.
C. Add static reset() { count = 0; } outside the class.
D. Add reset() { this.count = 0; } inside the class.

Solution

  1. Step 1: Define a static method inside the class

    Static methods must be declared with the static keyword inside the class.
  2. Step 2: Reset static property using this inside static method

    Inside a static method, this refers to the class, so this.count = 0; correctly resets the static property.
  3. Final Answer:

    Add static reset() { this.count = 0; } inside the class. -> Option B
  4. Quick Check:

    Static method + this resets static property [OK]
Hint: Static methods use static keyword and access static props via this [OK]
Common Mistakes:
  • Omitting static keyword on method
  • Using instance method to modify static property
  • Trying to reset static property outside class without static
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