Kanngongkanngoen

TypeScript and Object-Oriented Programming: A Practical Introduction

💡 Key takeaways

An introduction to object-oriented programming in TypeScript, including classes, types, the four common OOP principles, and how TypeScript relates to JavaScript.

Object-oriented programming (OOP) organizes code around objects that combine data and behavior. This introduction explains four OOP concepts and how TypeScript’s types and class syntax can be used when building JavaScript applications.

What is OOP?

In OOP, an object has attributes that describe it and methods that define what it can do. A calculator, for example, could be represented as an object with methods for addition, subtraction, multiplication, and division. Compared with a collection of unrelated functions, organizing related data and behavior together can make a codebase easier to navigate and maintain.

Design patterns are reusable ways to approach problems that occur in software design. They are often used with OOP, but they are not a substitute for choosing a design that fits the application.

Why use TypeScript with OOP?

TypeScript adds a type system and supports class-based syntax on top of JavaScript. Types can describe the expected values of variables and function parameters, helping a developer notice some mistakes while writing code. TypeScript code is compiled into JavaScript for a browser or Node.js environment.

The four common OOP principles

Encapsulation

Encapsulation groups related data and methods in a class and controls how the data is accessed. In TypeScript, access modifiers such as public, private, and protected describe access to class members. Getters and setters can provide controlled ways to read or update a value. JavaScript can use closures or other mechanisms to keep implementation details private.

Abstraction

Abstraction exposes the operations that other code needs while hiding implementation details. TypeScript interfaces and abstract classes can describe requirements that another class must implement. An abstract class is a base definition that cannot be instantiated directly.

Inheritance

Inheritance lets a class derive properties and methods from another class. The original class is often called the parent or base class; the derived one is a subclass. TypeScript uses the extends keyword for class inheritance. JavaScript also uses prototype-based inheritance.

Polymorphism

Polymorphism lets different objects respond to the same operation in their own way. A subclass may override a method from its parent class. In TypeScript, interfaces and classes can help define shared behavior for different implementations.

Classes and types in TypeScript

A TypeScript class can define an object’s fields and methods, with types describing the expected data. The compiler then converts TypeScript into JavaScript. TypeScript extends JavaScript with type-checking and other language features; as with any programming language, code still needs testing and careful review.

When these ideas are useful

OOP can help organize a program when related data and behavior belong together. Encapsulation can make access clearer, abstraction can reduce the amount of detail a caller needs to understand, inheritance can share behavior, and polymorphism can let code work with different implementations. These are design options, not rules that every program must follow.

For a free financial consultation with an expert, contact us on LINE.

KGK

Kanngongkanngoen Editorial Team

We create clear educational content about personal finance, investing, and debt management, with an emphasis on accuracy and transparency.