TypeScript Tutorial

TypeScript Object Oriented Programming

Objects bundle data and the functions that work on it. Classes are the blueprint.

Data and behavior together

So far a program has been variables at the top of the file and free functions that take those values. That works until related pieces scatter: a name in one variable, a score in another, a function that expects both in the right order.

Object oriented programming (OOP) groups those pieces. An object holds data and the functions that use that data. A class is the blueprint that names the type. You write the class once. Then you create as many objects of that type as you need with new.

Class vs object

The class is the plan. The object is a real value in memory. number is a type;let n: number = 3; makes a variable. Lamp is a type you define;const desk: Lamp = new Lamp(); makes an object.

ClassObject
What it isA type you defineA value of that type
When it existsIn the source, as a blueprintWhen the program creates it
How manyOne definitionAs many as you create
Exampleclass Lampnew Lamp()
AnalogyThe cookie cutterA cookie

A tiny class

Lamp has one field, on, and one method, toggle. The method belongs to the class. You call it on an object with the dot operator. This program creates one lamp, flips it, and prints the new state.

Example

class Lamp {
  public on: boolean = false;

  public toggle(): void {
    this.on = !this.on;
  }
}

const desk: Lamp = new Lamp();
desk.toggle();
if (desk.on) {
  console.log("on");
} else {
  console.log("off");
}

new Lamp() constructs the object. Inside toggle, this.on is that object’s field. The next chapters explain methods, constructors, and access in detail. Here the point is the bundle: data and the function that works on it live in one type.

Compile this in /typescript/try. Call toggle twice and watch the printed state change.

The same idea without a class

You could keep a let deskOn: boolean and a free function. The class version keeps the flag and the flip next to each other. When you add a second lamp, you do not invent a second pair of names. You create another Lamp.

Example

let deskOn: boolean = false;
deskOn = !deskOn;
console.log(deskOn);

That works for one flag. It does not scale when a lamp also needs a brightness, a room name, and three operations. The class is the type that holds those together.

What the next chapters cover

  • Classes and objects — define a type, create two objects with new, use public members.
  • Methods — functions that live on the class and run with the dot operator and this.
  • Constructors — code that runs when an object is created, so fields start valid.
  • Access specifierspublic, private, and protected.

Later chapters add encapsulation, inheritance with extends, and polymorphism withoverride. Those are still the same idea: a class names a type, objects are values of that type created with new.

Keep classes small

For this tutorial a class has a few fields and a few methods. Do not pack a whole program into one type. Name the class after the thing it models: Lamp, Player, Point. Next: how to write a class and create objects from it.

FAQ: TypeScript Object Oriented Programming

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript oop examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript oop in this TypeScript TypeScript lesson (TypeScript Object Oriented Programming).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.