惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Vercel News
Vercel News
F
Fortinet All Blogs
月光博客
月光博客
G
Google Developers Blog
博客园 - Franky
GbyAI
GbyAI
The Cloudflare Blog
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
小众软件
小众软件
腾讯CDC
B
Blog
量子位
V
V2EX
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
TypeScript. Object-oriented programming (OOP)
Julia Shlykova · 2026-06-19 · via DEV Community

Julia Shlykova

When we hear about OOP, what is the first thing that comes to mind? Classes!

Basic structure

Classes in TypeScript look pretty the same as in JavaScript only with specifying types.

class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  greet() {
    console.log(`Welcome, ${this.name}!`);
  }
}

const p1 = new Person("Mary");

p1.greet(); // Welcome, Mary!


Access modifiers

We can restrict or open access to specific properties or methods by using keywords:

Private modifier

The private modifier makes a property available only within the class.

class Person {
  private name: string;
  constructor(name: string) {
    this.name = name;
  }
  greet() {
    console.log(`Welcome, ${this.name}!`);
  }
}

const p1 = new Person("Mary");

console.log(p1.name); // Property 'name' is private and only accessible within class 'Person'

But how would we read it ouside the class? Here comes getter:

class Person {
  private name: string;
  constructor(name: string) {
    this.name = name;
  }
  greet() {
    console.log(`Welcome, ${this.name}!`);
  }
  getName() {
    return this.name;
  }
}

const p1 = new Person("Mary");

console.log(p1.getName()); // Mary

We can also use setter to change the private property (we can also set some conditions):

class Person {
  private name: string;
  constructor(name: string) {
    this.name = name;
  }
  greet() {
    console.log(`Welcome, ${this.name}!`);
  }
  setName(name: string) {
    if (name.length < 5) return;
    this.name = name;
  }
}

Public modifier

Actually, this value is there by default.

class Person {
  public name: string;
  constructor(name: string) {
    this.name = name;
  }
  greet() {
    console.log(`Welcome, ${this.name}!`);
  }
}

const p1 = new Person("Mary");

console.log(p1.name); // Mary

Protected modifier

The protected modifier makes a property available only within the class and its subclasses:

class Person {
  protected name: string;
  constructor(name: string) {
    this.name = name;
  }
}

class User extends Person {
  greet() {
    console.log(`Hello there, ${this.name}`);
  }
}

const p1 = new User("Mary");

console.log(p1.greet());

Readonly modifier

The readonly modifier prevents the property from being modified outside of the constructor:

class Person {
  readonly name: string = "No name";
  constructor(otherName: string) {
    this.name = otherName;
  }
  changeName(otherName: string) {
    this.name = otherName; // Cannot assign to 'name' because it is a read-only property
  }
}

const p1 = new Person("Mary");
p1.name = "Ann"; // Cannot assign to 'name' because it is a read-only property

We can also use it for interfaces:

interface User {
  readonly password: string;
  name: string;
}

let user: User = {
  password: 'password',
  name: 'John Smith'
}

user.name = 'Mary Smith';

user.password = 'newPassword'; //Cannot assign to 'password' because it is a read-only property.


Abstract class

This is a restricted class (we can't create instances from it), from which we can create subclasses. It's usually used to define mandatory methods.

abstract class Dog {
  abstract bark(duration: number): void;

  walk(duration: number) {
    console.log("Walking");
    this.bark(duration);
  }
}

class Husky extends Dog {
  bark(duration: number) {
    console.log("Wooooooo");
  }
}

class Chihuahua extends Dog {
  bark(duration: number) {
    console.log("wof wof wof");
  }
}

let d1 = new Husky();

d1.walk(2);

Here, subclasses must implement their own bark method, since it's defined with keywork abstract. They inherited the method walk.


Classes and interfaces

Classes can implement an interface, that allows to treat instances from different classes (that implement the same interface) as the same object hiding complexity, that we don't care about it at this moment:

interface MakeSound {
  makeSound(): void;
}

class Python implements MakeSound {
  length: number;

  constructor(length: number) {
    this.length = length;
  }

  makeSound() {
    console.log('Ssssss!');
  }
}

class Puma implements MakeSound {
  makeSound() {
    console.log('Roar!');
  }
}

const python = new Python(10);
const puma = new Puma();

function animalSpeak(animal: MakeSound) {
  animal.makeSound();
}

animalSpeak(python);
animalSpeak(puma);

function animalSpeak is interested only in the ability of object to makeSound, it doesn't care if the object is python or puma.


Static method and field

Like in JavaScript, we can define static methods and fields for a class. They cannot be accessed on instances, but on the class itself. We can use it to create some shared values or maybe count instances that exist:

class Person {
  static instanceCount: number = 0;
  name: string;

  constructor(name: string) {
    Person.instanceCount++;
    this.name = name;
  }
}

const p1 = new Person('Mary');
const p2 = new Person('Tom');
console.log(Person.instanceCount); // 2

We also can define static method:

class Person {
  static instanceCount: number = 0;
  name: string;

  constructor(name: string) {
    Person.instanceCount++;
    this.name = name;
  }

  static clearCount() {
    this.instanceCount = 0;
  }
}

const p1 = new Person('Mary');
const p2 = new Person('Tom');
Person.clearCount();
console.log(Person.instanceCount); // 0

Static methods can only access variables that associate with the class.