Skip to content

TypeScript Classes for Beginners: Fields, Constructors, private, readonly and implements

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

JavaScript classes bundle data with the functions that work on it: a cart with its lines and a total() method, a wallet with its balance and pay(). TypeScript adds three things to them. Every field is declared with a type, so nothing appears on this by surprise. Access modifiers say which parts other code may touch, so a balance cannot be changed from outside except through the methods you allow. And implements checks that a class really has the shape an interface promises. You will meet classes in Angular, in NestJS on the server, in error types and in many libraries, so even if you prefer plain functions, you need to read them comfortably.

Quick answer

Declare each field with its type, such as items: CartLine[] = []; strict mode requires a value or an assignment in the constructor. constructor(public readonly owner: string) declares and assigns a field in one go. private hides a member from other code at compile time only, while #balance is private at run time too. readonly blocks reassignment after construction. class Card implements PaymentMethod makes the compiler check the class against the interface. Use abstract for a base class that must not be created directly, and override when a subclass replaces a method.

35 min read and practice · Content checked on 18 November 2026

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.

Try it: A cart class with typed fields and a getter
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.