Topic Overview

Welcome to an exciting exploration of TypeScript classes, where we delve into constructors and class methods. Imagine building a robot: Constructors establish its initial state, and class methods instruct it to perform tasks. Ready to deepen your understanding? Let’s get started!

Revisiting TypeScript Classes

A TypeScript class serves as a blueprint for creating objects and incorporates static typing for both properties and methods. Here's a basic class, Robot, with type annotations:

Initially, this Robot class is an empty shell. To make it functional, we need to define properties with explicit types, as well as methods.

Deep Dive into Constructors

A constructor is a unique method in TypeScript that sets up an object when an instance is created. You'll use the constructor keyword to define it, and it is automatically invoked during the instance creation process. If no constructor is explicitly defined, TypeScript provides a default constructor, allowing instance creation without initializing specific properties. By using static typing within the constructor, as shown in the code block, you ensure that the parameters have specified types, which aids in maintaining consistency and reducing errors when initializing object properties.

Here, we enhance the Robot class by adding a constructor:

In this case, the constructor method gets automatically called when we create a new Robot instance, set with name and color attributes. When creating an instance of Robot, the constructor initializes properties in the order they are defined in the constructor.

Multiple Constructors with Default Parameters

While TypeScript does not support multiple constructors, you can attain flexibility using default parameters, where a parameter is given a default value if no value or undefined is provided. Type annotations play a crucial role here, guaranteeing that parameters have defined types, even when default values are utilized.

Here's a default color for our robots:

With default parameters, the color becomes optional, defaulting to grey if not provided, maintaining type safety. As a result, the robot instance Bobby is assigned the color grey by default.

Class Methods

Class methods in TypeScript allow objects to perform actions. Annotating method parameters and return types reinforces type safety. This reduces the risk of runtime errors and makes the code more predictable and easier to maintain:

Here, the sayHello method enables our robot instance to interact using specified types.

Updating and Retrieving Parameters with Class Methods

Methods allow the modification and retrieval of class instance attributes. While you can access attributes directly in the provided example, as the attributes are public, using setter and getter methods can enhance safety by providing controlled access and validation. Type annotations ensure type consistency, further safeguarding data integrity.

The setName and setColor methods allow changing the name and color of the robot after it has been created. The getName and getColor methods enable retrieving these values when needed.

Lesson Summary

Well done! You've explored TypeScript classes, constructors, and class methods, emphasizing the importance of static typing through type annotations. This approach enhances code reliability and readability, allowing for more nuanced control over your classes. Moving forward, you'll have the skills needed to infuse more sophistication into your TypeScript projects, making them both robust and versatile. Let's continue to rebuild our coding toolbox with practical exercises in the upcoming lessons!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal