typescript
/

TypeScript Classes

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Classes

What is a Class?

A class in TypeScript is a blueprint for creating objects, supporting object-oriented programming features like encapsulation and inheritance.

Basic Class Example

TypeScriptRead-only
1
class User {
  name: string;

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

const u = new User('John');

Access Modifiers

TypeScriptRead-only
1
class Person {
  public name: string;
  private age: number;
  protected id: number;
}

Readonly Properties

TypeScriptRead-only
1
class User {
  readonly id: number;

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

Methods

TypeScriptRead-only
1
class User {
  greet(): string {
    return 'Hello';
  }
}

Inheritance

TypeScriptRead-only
1
class Animal {
  move() {
    console.log('Moving');
  }
}

class Dog extends Animal {
  bark() {
    console.log('Bark');
  }
}

Constructor Short Syntax

TypeScriptRead-only
1
class User {
  constructor(public name: string) {}
}

Best Practices

  • Use access modifiers properly
  • Keep classes focused and simple
  • Use inheritance wisely
  • Encapsulate logic inside classes

Common Mistakes

  • Overusing inheritance
  • Not using access modifiers
  • Large and complex classes
  • Ignoring encapsulation

Conclusion

TypeScript classes enable object-oriented programming, helping build scalable and maintainable applications.

Try it yourself

class User { constructor(public name: string) {} }

Test Your Knowledge

Q1
of 3

Class used for?

A
Styling
B
Object creation
C
Routing
D
API
Q2
of 3

Private means?

A
Public
B
Accessible everywhere
C
Restricted
D
Global
Q3
of 3

Inheritance keyword?

A
extends
B
implements
C
class
D
new

Frequently Asked Questions

What is class?

Blueprint for creating objects.

Access modifiers?

public, private, protected.

What is inheritance?

Extending one class from another.

Previous

ts tuples

Next

ts access modifiers

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.