What are private and protected members in Typescript?

Private Members Private members are only accessible from within the class they are declared. They cannot be accessed from outside the class. There are also not accessible from any subclasses that inherit them. class Animal { private weight: number; private height: number; constructor(weight: number, height: number) { this.weight = weight; this.height = height; } } let animal = new Animal(1, 2); console.log(animal); // Animal {weight: 1, height: 2} Now if I want to access the weight or height property from outside the class, Typescript will throw an error....

February 4, 2023 · 5 min

How to override a method in Typescript?

You can override a method in Typescript by creating a new method inside the inheriting class with the same name as that of the parent method. This way you can replace the original method declared in the parent class with the new method inside the child class. class Animal { kingdom() { console.log("I belong to Animal Kingdom"); } } class Dog extends Animal {} In the above example, class Dog inherits from class Animal....

February 4, 2023 · 1 min

How to make use of inheritance in TypeScript?

TypeScript language has support for inheritance where a class can inherit from another class. Typescript uses extends keyword to implement inheritance. class Parent {} class Child extends Parent {} The class Child inherits class Parent. This allows Child class to inherit all the properties and methods of Parent class. Let’s look at inheritance in more detail using a class called Animal. class Animal { weight: number; height: number; constructor(weight: number, height: number) { this....

February 3, 2023 · 3 min

What is a readonly modifier in Typescript?

A readonly modifier is used in Typescript to specify a property as read-only, that is, the value of the property cannot be changed once it is initialized. This way we can make a property of a class immutable. To declare a property as read-only we need to prefix it with the keyword readonly. class Student { readonly id: number; name: string; constructor(id: number, name: string) { this.id = id; this.name = name; } } let student = new Student(1, "Bob"); console....

February 3, 2023 · 1 min

How to declare and use public and private access modifiers in TypeScript?

Public access modifier “Public” is the default access modifier that can be accessed from anywhere. We don’t have to explicitly specify a public keyword when declaring a property or a method in Typescript. class Student { name: string; //public by default constructor(name: string) { this.name = name; } } let student = new Student("Bob"); console.log(student); // Student {name: 'Bob'} Here, name property is public by default and it can be accessed from within the class as well as outside the class....

February 3, 2023 · 3 min

How to declare static methods and properties in Typescript?

We make use of the keyword static to declare static methods and static properties in Typescript. In this article, we will look at how to declare and use them in TypeScript. Static Methods To declare a static method we need to prefix the method with static keyword. Let us first create a regular method and access it. class Student { greet() { console.log("Hello there!"); } } let student = new Student(); student....

February 2, 2023 · 2 min

How to Declare a Method Inside a Class in Typescript?

In typescript, we can declare methods inside a class using the following syntax: class Myclass { myMethod() {} } Let’s say we have a class Student and we want a method that greets a student. To do so we can declare a greet() method as follows. class Student { greet() { console.log("Hello!"); } } let student = new Student(); console.log(student.greet()); // Hello! To call the method, we first create an object student and call the method using student....

February 2, 2023 · 2 min

How to Declare a Class in Typescript?

To declare a class in TypeScript, we make use of the class keyword followed by the class name. class Student {} This creates an empty class. Adding fields in a class Fields are properties of a class that can be used to assign values. Let’s declare two fields name and age for our Student class. class Student { name: string; age: number; } Here, we have declared the type of name to be a string and the age to be a number....

February 2, 2023 · 2 min

How to integrate React Frontend with NodeJs Backend?

In this article, we will look at how to connect React with the Node Js server. React will be our front-end library and Node Js will serve as the backend server. Let’s begin by creating a node Js server. We will be using express which will help us create a web server quickly. I will create an empty folder that will have all the Node Js code in it. mkdir react-node-app Navigate to the new folder react-node-app and run the npm init command....

November 2, 2022 · 5 min

NextJs: How to fetch data from an API?

We will start by creating a NextJs app using the creat-next-app command. I will be using npx to create a new project called my-pokemon-app. For this tutorial, we will be making use of the Pokemon API(https://pokeapi.co/) to demonstrate how to consume external APIs using NextJs. npx create-next-app@latest my-pokemon-app Let’s navigate to our project and open the code in the editor of our choice. cd my-pokemon-app You can notice, the create-next-app creates a bunch of folders and files necessary for us to run a NextJs project....

October 29, 2022 · 5 min