Lesson 31 of 55
15 mins readPython Class Fields, Methods, and Constructors
In Plain English
TypeScript brings type annotations and strict initialization checks to modern ES6 classes, ensuring robust Object-Oriented patterns.
Core Rules to Remember

Field Annotations: Statically types instance variables and validates constructor initialization.

Method Signatures: Enforces type-safe parameters and return types across class methods.
Live Interactive Example
Hit Run Code to see it liveDeclaring Fields and Constructors
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: Class fields are verified against constructor assignments and method return types are strictly enforced.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Build a Counter Class
Create class StepCounter with field count: number initialized to 0 and step: number in constructor.
Add method increment(): number that increases count by step and returns count.
Instantiate with step 5, call increment() twice, and print count.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.