Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
TypeScript Intermediate FREE

Classes

Lesson 1 of 17 Intermediate Interactive

Classes define blueprints for objects with properties and methods.

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.

Interview Questions

It is a fundamental TypeScript feature.