TypeScript: How to declare optional parameters in functions?

To declare a parameter of a function as optional in TypeScript, we need to add ? after the parameter name. For example, if a parameter name is id and we want it to be optional then we need to write it as id?. Let’s understand it better by declaring a simple greet function that takes an argument message and logs it. function greet(message: string) { console.log(message); } greet("Hello"); // Hello Here, we need to pass message of type string or Typescript will complain about it....

February 6, 2023 · 1 min

How to define function types in TypeScript using interfaces?

In TypeScript, it is possible to define the structure of a function using an interface by defining: the type of arguments the function can have and the type of return value a function can have. In this article, we will look at how to declare an interface as a function type. interface Sum { (a: number, b: number): number; } let sum: Sum = (a: number, b: number) => a + b; console....

February 6, 2023 · 2 min

How to implement multiple interfaces in a class in TypeScript?

A class in TypeScript can implement multiple interfaces using the implements keyword with the names of interfaces separated by a comma. interface Name { name: string; } interface Weight { weight: number; } interface Height { height: number; } class Animal implements Name, Weight, Height { name: string; height: number; weight: number; constructor(name: string, weight: number, height: number) { this.name = name; this.weight = weight; this.height = height; } } let animal = new Animal("Bruno", 5, 3); console....

February 6, 2023 · 2 min

How to extend multiple interfaces in TypeScript?

It is possible to inherit one interface from another using the extends keyword. This makes it possible to extend multiple interfaces in Typescript. Let’s look at how to implement this in Typescript. interface Name { name: string; } interface Weight { weight: number; } interface Height { height: number; } interface Animal extends Name, Weight, Height {} let animal: Animal = { name: "Bruno", weight: 5, height: 3, }; console.log(animal); // {name: 'Bruno', weight: 5, height: 3} In the above example, we have four interfaces, Name, Weight, Height, and Animal....

February 6, 2023 · 2 min

TypeScript: How to make a property read-only in interface?

By default, all the properties declared in the interface are public but you can make a property read-only by using the readonly keyword. This means the value of the property cannot be changed once it is initialized. This way we can make a property of an interface immutable. Let’s create an Animal interface that has two properties height and weight. Next, we will create a dog object of type Animal and assign values....

February 5, 2023 · 2 min

What is an interface and how to implement it in Typescript?

An Interface is used to define a structure for an object. Basically, it is used to specify the properties and methods an object must implement. interface Student { id: number; name: string; } let student: Student = { id: 1, name: "Bob", }; In the above example, we declare an interface Student with two properties id and number. Next, we create a variable student which of type Student and assign an id and name....

February 5, 2023 · 2 min

How to write constructor shorthand in TypeScript?

In this article, we will look at how to write less code when declaring a class using the constructor shorthand available in Typescript. We will first start by declaring a regular class. class Student { name: string; constructor(name: string) { this.name = name; } } let student = new Student("Bob"); console.log(student); // Student {name: 'Bob'} We have a class called Student with the property field name which is public. We can write the same class using the shorthand as follows:...

February 4, 2023 · 2 min

How to declare and use private constructors in TypeScript?

To declare a private constructor in TypeScript, we need to prefix private keyword to the constructor declaration. A private constructor cannot be created from outside of the class. It is also important to note that when we declare a private constructor, we cannot create multiple instances of a class (singleton pattern). class Singleton { private constructor() {} } let singleton = new Singleton(); // NOT ALLOWED Typescript will throw an error, Constructor of class 'Singleton' is private and only accessible within the class declaration....

February 4, 2023 · 2 min

What are abstract classes and how to implement them in TypeScript?

An abstract class is a base class that contains common implementation details that can be used by the derived class. An abstract class cannot be instantiated on its own. To declare a class as abstract, we can use abstract keyword followed by the class definition. abstract class Animal { } We cannot instantiate an abstract class, for example, the below code will throw an error. // NOT ALLOWED let animal = new Animal(); // Cannot create an instance of an abstract class If an abstract class contains abstract methods then it must be implemented by all the derived classes....

February 4, 2023 · 2 min

How to implement getter and setter methods in Typescript?

The getter method is used when you want to access a property of an object whereas, setter method is used to set or change a value of a property of an object. Typescript uses get keyword to declare a getter method and set keyword to declare a setter method. class Animal { private weight: number; get myWeight() { return this.weight; } set myWeight(weight: number) { if (weight > 0) { this....

February 4, 2023 · 2 min