0Pricing
TypeScript Academy · Lesson

TypeScript Classes and Constructors

Define classes with typed properties and constructors.

TypeScript Classes and Constructors is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript classes are JavaScript classes with type annotations. They combine data and behavior in a typed, object-oriented structure.

Class Declaration with Properties

Declare class properties at the top of the class with their types. TypeScript checks all usages.
class Person {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

Parameter Properties Shorthand

Prefix constructor parameters with an access modifier to automatically create and initialize a class property.
class Person {
  constructor(
    public name: string,
    private age: number
  ) {} // properties created automatically
}

Typed Methods

Class methods use the same parameter and return type annotations as regular functions.
class Calculator {
  add(a: number, b: number): number {
    return a + b;
  }
}

Getters and Setters

Use get and set keywords to create computed properties. TypeScript checks their types.
class Circle {
  constructor(private radius: number) {}
  get area(): number { return Math.PI * this.radius ** 2; }
  set r(v: number) { this.radius = v; }
}

Class as a Type

A class defines both a value (constructor function) and a type (instance shape). Use the class name as a type annotation.
class Dog { name = 'Rex'; bark() { return 'Woof'; } }
const d: Dog = new Dog();

Static Members

Static properties and methods belong to the class itself, not to instances. Access them via the class name.
class MathHelper {
  static PI = 3.14159;
  static square(n: number): number { return n * n; }
}
console.log(MathHelper.PI);

Class Inheritance

Use `extends` to inherit from a base class. Call `super()` in the constructor to initialize the parent.
class Animal {
  constructor(public name: string) {}
  speak(): string { return '...'; }
}
class Dog extends Animal {
  speak(): string { return 'Woof!'; }
}

Abstract Classes

Abstract classes cannot be instantiated. They define a contract that subclasses must implement.
abstract class Shape {
  abstract area(): number;
  describe(): string { return `Area: ${this.area()}`; }
}
class Square extends Shape {
  constructor(private side: number) { super(); }
  area() { return this.side ** 2; }
}

this Type in Classes

The `this` type refers to the current instance type, enabling fluent/builder APIs with correct return types.
class Builder {
  private parts: string[] = [];
  add(p: string): this { this.parts.push(p); return this; }
  build(): string { return this.parts.join(','); }
}

Class vs Interface vs Type

Classes are runtime values with constructors. Interfaces and type aliases are compile-time-only type shapes. Use classes when you need instantiation and methods.

Quick Check

What is the effect of prefixing a constructor parameter with `public` in TypeScript?

Recap

TypeScript classes add types to JavaScript classes. Use parameter properties for concise declarations, access modifiers for encapsulation, and abstract classes for enforcing subclass contracts.

Frequently asked questions

Is the “TypeScript Classes and Constructors” lesson free?

Yes — the full text of “TypeScript Classes and Constructors” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “TypeScript Classes and Constructors”?

Define classes with typed properties and constructors. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “TypeScript Classes and Constructors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. TypeScript Classes and Constructors
  2. public, private, and protected Modifiers
  3. readonly Properties and Parameter Properties
  4. Implementing Interfaces with Classes
← Back to TypeScript Academy