A static member is a property or method that belongs to the class itself, not to any instance of the class. You can access it using the class name without creating an object.
Click to reveal answer
beginner
How do you declare a static property with a type in TypeScript?
Use the static keyword before the property name and specify its type like this: static count: number;
Click to reveal answer
intermediate
Can static methods access instance properties directly? Why or why not?
No, static methods cannot access instance properties directly because they belong to the class, not to any specific object. Instance properties need an object to exist.
Click to reveal answer
beginner
How do you call a static method in TypeScript?
You call it using the class name followed by the method name, like <code>ClassName.methodName()</code>, without creating an instance.
Click to reveal answer
intermediate
What is the benefit of typing static members in TypeScript?
Typing static members helps catch errors early by ensuring the correct data types are used. It also improves code readability and helps tools provide better suggestions.
Click to reveal answer
How do you access a static property named total in a class called Counter?
ACounter.total
Bthis.total
Cnew Counter().total
Dtotal
✗ Incorrect
Static properties are accessed using the class name, so Counter.total is correct.
Which keyword is used to declare a static member in TypeScript?
Astatic
Bconst
Clet
Dreadonly
✗ Incorrect
The static keyword declares static members in a class.
Can a static method access instance properties directly?
AYes, always
BOnly if the method is marked as private
COnly if the instance is passed as a parameter
DNo, because static methods belong to the class, not instances
✗ Incorrect
Static methods cannot access instance properties directly because they do not belong to any object instance.
What happens if you try to access a static property through an instance?
AIt deletes the static property
BIt works normally
CIt will cause a TypeScript error
DIt creates a new property on the instance
✗ Incorrect
Accessing static properties through instances is not allowed and TypeScript will show an error.
Why is it useful to type static members in TypeScript?
ATo allow static members to change types dynamically
BTo catch type errors early and improve code clarity
CTo make the code run faster
DTo prevent the class from being instantiated
✗ Incorrect
Typing static members helps catch mistakes early and makes the code easier to understand.
Explain what static members are in TypeScript and how you declare them with types.
Think about how static members belong to the class, not instances.
You got /3 concepts.
Describe how to access static members and why static methods cannot access instance properties directly.
Remember static means 'belonging to the class itself'.
You got /3 concepts.
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
Step 1: Understand what static means
Static members belong to the class itself, not to any object created from the class.
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.
Final Answer:
Static members belong to the class itself, not to instances. -> Option C
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
Step 1: Recall static property syntax
In TypeScript, static properties are declared with the keyword static before the property name and type.
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.
Final Answer:
static count: number = 0; -> Option A
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
Step 1: Understand static property usage
The static property count belongs to the class Counter, shared by all instances.
Step 2: Trace the increments
Both c1.increment() and c2.increment() increase Counter.count by 1 each, so total increments = 2.
Final Answer:
2 -> Option D
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
Step 1: Understand static property access
Static properties belong to the class, so they must be accessed via the class name, not this.
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.
Final Answer:
Cannot assign to static property using this inside constructor. -> Option A
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
Step 1: Define a static method inside the class
Static methods must be declared with the static keyword inside the class.
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.
Final Answer:
Add static reset() { this.count = 0; } inside the class. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.