In this lesson you will
- Declare typed fields, constructors and parameter properties
- Choose between public, private, protected, #private and readonly
- Check a class against an interface with implements
- Decide when a class is better than plain objects and functions
Fields, constructors and parameter properties
In plain JavaScript a class can create properties anywhere, just by assigning this.something. TypeScript wants each field declared at the top of the class with its type: items: CartLine[] = []; or currency: Currency;. In strict mode, a field must get a value, either right there or in the constructor; otherwise you get the "has no initializer" error from the last lesson. Methods are typed like functions, and a getter, get total(): number, is read like a property, cart.total, without brackets.
A constructor that only copies its arguments into fields is so common that TypeScript has a shortcut. Put a modifier in front of a parameter, as in constructor(readonly currency: Currency), and TypeScript declares the field and assigns it for you. This is called a parameter property. It saves lines, but it is one of the few TypeScript features that generates JavaScript, so it is rejected by the erasableSyntaxOnly option from the last lesson; projects that run TypeScript through Node.js type stripping write the field and the assignment out in full.
Console
- Console messages, errors, form data and clicked links appear here.
Line 12 declares and assigns currency in one go. The last two lines are rejected: currency is readonly, and a line without qty does not match CartLine. The JavaScript still runs, which is why the total is printed before those lines.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between private and # in TypeScript?
private is checked only by the TypeScript compiler and is removed from the JavaScript, so the field is still reachable at run time. #field is a JavaScript feature enforced by the engine, so code outside the class cannot read it at all, even with bracket access.
What is a parameter property in TypeScript?
A constructor parameter with a modifier such as readonly, public or private. TypeScript turns it into a field and assigns the argument automatically. It generates code, so it is not allowed with the erasableSyntaxOnly option.
Should I use an interface or an abstract class?
Use an interface to describe a shape that many unrelated things can match; it costs nothing at run time. Use an abstract class when subclasses should also share real code, such as a common method, because an interface cannot hold implementation.
What does the override keyword do?
It marks a method that replaces one from the base class. If the base class has no such method, for example after a rename, TypeScript reports an error. With noImplicitOverride on, replacing a method without the keyword is an error too.
Do I need classes to use TypeScript?
No. TypeScript works just as well with plain objects, interfaces and functions, and much modern code is written that way. Learn classes so you can read frameworks and libraries that use them, and pick them when data and behaviour truly belong together.
Sources
Facts in this lesson were checked against these official pages on 18 November 2026.
- TypeScript Handbook: Classes
- MDN: Private properties
- TSConfig reference: noImplicitOverride
- TypeScript 5.8 release notes: erasableSyntaxOnly
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.