0
0
Typescriptprogramming~5 mins

Static members with types in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a static member in a TypeScript class?
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
Which keyword is used to declare a static member in TypeScript?
Astatic
Bconst
Clet
Dreadonly
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
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
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
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.