0
0
Typescriptprogramming~15 mins

Static members with types in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Static members with types
📖 Scenario: You are creating a simple class to keep track of how many times a certain action happens across all instances.
🎯 Goal: Build a TypeScript class with a static member that counts how many times a method is called on any instance.
📋 What You'll Learn
Create a class called ActionCounter
Add a static number property called count initialized to 0
Add a method called performAction that increases count by 1
Use proper TypeScript types for the static member and method
Print the static count after calling performAction multiple times
💡 Why This Matters
🌍 Real World
Static members are useful to keep track of data shared across all objects, like counting total users logged in or tracking global settings.
💼 Career
Understanding static members helps in designing classes that manage shared data efficiently, a common task in software development.
Progress0 / 4 steps
1
Create the class with a static count property
Create a class called ActionCounter with a static property count of type number initialized to 0.
Typescript
Need a hint?

Use static keyword before the property name and specify the type number.

2
Add the performAction method
Inside the ActionCounter class, add a method called performAction that increases the static count by 1.
Typescript
Need a hint?

Use ActionCounter.count inside the method to access the static property.

3
Create instances and call performAction
Create two instances of ActionCounter named first and second. Call performAction once on first and twice on second.
Typescript
Need a hint?

Create instances with new ActionCounter() and call the method with dot notation.

4
Print the static count
Print the value of the static count property of ActionCounter using console.log.
Typescript
Need a hint?

Use console.log(ActionCounter.count) to print the static count.