TypeScript Classes: private, #private, abstract, implements
Access modifiers vs #private fields, parameter properties, implements, abstract classes and this types, with what each emits.
In this chapter
Classes in TypeScript
More on Everyday types
- Why TypeScript? Benefits Over JavaScript, Explained
- TypeScript Types: Primitives, Arrays and Tuples
- TypeScript Function Types: Parameters, Overloads, void
- TypeScript Interface vs Type Alias, and Object Types
- Structural Typing and Excess Property Checks in TypeScript
- any vs unknown vs never in TypeScript
- TypeScript readonly, as const and Type Widening
- TypeScript Enums vs Union Types: Should You Use Enums?
- Annotate or let TypeScript infer
Keep learning
Overview
chapters · everyday types Classes in TypeScript JavaScript classes plus a type layer: access modifiers, readonly, implements, abstract and this types.
1. The problem: TypeScript's private is not truly private
private: a type-check only Property '#balance' is not accessible outside class 'Wallet' because it has a private identifier. The # is real JavaScript, not just TypeScript. The engine itself hides the field, so line 7 fails and JSON.stringify cannot see it.
2. Access modifiers: who may read a field
An access modifier is a word before a field that says who may use it. extends makes a subclass: a class that inherits everything from another and adds more. Here Savings is a subclass of Account. Property 'limit' is protected and only accessible within class 'Account' and its subclasses. Property 'pin' is private and only accessible within class 'Account'. Line 11 fails: limit is protected, so only Account and its subclasses may use it, and line 11 is outside both. Line 12 fails: pin is private, so even the subclass cannot touch it. Only owner (public) works everywhere. Inside the class In a subclass obj['x'] escape hatch Enforced at runtime Use #private when it truly must stay hidden. Use private for a lighter, type-only promise.
4. Fields must be initialized; readonly ones only once
Property 'code' has no initializer and is not definitely assigned in the constructor. Cannot assign to 'createdAt' because it is a read-only property. Line 2 fails: code is typed string, but nothing ever gives it a value. It would start as undefined, which is not a string. Line 7 fails: a readonly field may be set only where it is declared or inside the constructor. reissue() is neither. Line 4's ! (a definite assignment assertion) tells TypeScript "I promise this gets set", so no error there. Assign in constructor Every path must set it. Definite assignment ! You promise. TypeScript stops checking.
5. implements checks the class but does not fill in its types
An interface describes a shape: which methods and fields an object must have. class X implements Notifier asks TypeScript: "check that X has that shape". It does not copy the interface's parameter types into the class. untyped params Parameter 'to' implicitly has an 'any' type. (Same error for 'body'.) "Implicitly has an any type" means TypeScript couldn't work out a type and gave up. The types from Notifier are not copied into the method, so you must write to: string, body: string yourself. fewer params wrong param type Property 'send' in type 'PushNotifier' is not assignable to the same property in base type 'Notifier'. … Types of parameters 'to' and 'to' are incompatible. Type 'string' is not assignable to type 'number'. Caught. Code that holds a Notifier passes a string as to, but this method expects a number. "Not assignable to" means "doesn't fit into". Without implements, the class itself shows no error. The mismatch appears later, on the line where you pass a PushNotifier to something that expects a Notifier.
6. Abstract classes and override
Cannot create an instance of an abstract class. Non-abstract class 'Card' does not implement inherited abstract member charge from class 'PaymentMethod'. Line 6 fails because PaymentMethod is unfinished: charge has no body to run. Line 8 fails because Card is a normal class but never writes the missing charge body. Shape only. No code, nothing at runtime. A class can implement many. abstract class Shape plus shared code (describe). A class extends only one.
Same PaymentMethod as above, with noImplicitOverride: true
This member must have an 'override' modifier because it overrides a member in the base class 'PaymentMethod'. This member cannot have an 'override' modifier because it is not declared in the base class 'PaymentMethod'. noImplicitOverride is a compiler setting. With it on, replacing a parent's method requires the word override. Line 3 fails: describe replaces the parent's describe without saying override. Line 4 fails: refund says override, but the parent has no refund to replace. So if someone renames or removes a parent method, every subclass that relied on it gets an error. Filling in an abstract method (line 2) does not need override.
How TypeScript types the chain
new SqlBuilder() If where returned QueryBuilder instead, step 2 would give QueryBuilder. The parent class has no limit, so you would get: Property 'limit' does not exist on type 'QueryBuilder'.
Remember
Parameter properties declare and assign a field from the constructor. implements checks the class but does not type its method parameters. Return this from chainable methods so subclasses keep their own type. ! Cheat code Use #private for real secrets. Annotate methods even with implements. Turn on noImplicitOverride. → Go deeper: parameter properties and erasable syntax, generic classes and builder chains, mixins, decorators.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.