Classes define blueprints for objects with properties and methods.
1. Getting Started
What is TypeScript?
2. Basic Types
Types & Interfaces
9. Async TypeScript
Promises & Async/Await
Syntax
TYPESCRIPT
class Animal { name: string; constructor(name: string) { this.name = name; } speak(): string { return `${this.name} makes a sound`; } }
TypeScript Classes
TYPESCRIPT
class Animal { constructor( public name: string, private sound: string ) {} speak(): string { return `${this.name} says ${this.sound}`; } } class Dog extends Animal { constructor(name: string) { super(name, "Woof"); } fetch(): string { return `${this.name} is fetching!`; } } const dog = new Dog("Buddy"); console.log(dog.speak()); console.log(dog.fetch());
Practice
1
Exercise
Practice this concept.
Answer
Write the code as shown above.
Quick Quiz
1
What did you learn?
This covers the basics.
Choose an answer to continue.
Interview Questions
It is a fundamental TypeScript feature.