Found 128 Articles for TypeScript

How to find the length of a string in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 15:56:59

3K+ Views

The string can contain a sequence of repeated and non-repeated characters. The length of the string means the total number of characters the string contains in the sequence of characters, including special characters, alphabetic characters, etc. Here, we will learn different methods to calculate the length of the string in TypeScript. Using the length property of the string In TypeScript, the string is a library, or we can say it is a class. It contains some properties and methods, which we can call by taking the string class object as a reference. The length is also the property of the ... Read More

Handling Errors in TypeScript

Shubham Vora
Updated on 19-Dec-2022 15:53:39

6K+ Views

Functional programming languages such as TypeScript and JavaScript provide a way to handle errors using the try-catch blocks. The try block catches the errors, and the catch block handles the errors.  In this tutorial, we will learn to handle errors in TypeScript. There are mainly 7 types of errors in TypeScript. Here, we will learn all types of errors one by one. Range error − If we try to access anything out of range, TypeScript will throw a range error. For example, accessing the array index for a large number value such as 10^100. Reference error − If we ... Read More

How to Convert Date to String in TypeScript?

Shubham Vora
Updated on 31-Oct-2023 21:15:31

52K+ Views

It is common to show the date and time on the web or mobile application. As a user, have you ever seen any application which shows the date object as it is? It will never happen because it makes UX worst. So, we need to convert the date object to a string. After converting the date object to a string, we can format it according to our needs. The date format means ‘yyyy-mm-dd’, ‘dd-mm-yyyy’, etc. Also, we can do the same with the time string. We can display the hours, minutes, seconds, and milliseconds according to the need. In this ... Read More

How to use property decorators in typescript?

Shubham Vora
Updated on 19-Dec-2022 15:49:48

3K+ Views

Decorators in TypeScript have programmatic access to the class definition process. Remember that a class description lists a class's properties, specified methods, and structure. The class instance is given access to these properties and methods when created. However, before a class instance is created, decorators allow us to add code to the definition of a class. They are equivalent to C# attributes or Java annotations. A decorator is a function that has a particular set of parameters. The JavaScript runtime automatically fills out these parameters, which give information about the class, method, or property to which the decorator has been ... Read More

How to use Hashmap in TypeScript?

Shubham Vora
Updated on 07-Oct-2023 03:14:43

30K+ Views

The Hashmap is the one kind of data structure that stores the key-value pairs of the different data.  Like other programming languages, TypeScript also contains a built-in map data structure. In JavaScript, we can't define the key or value type that needs to be stored in the map. So, we need to create a map of the generic type. In TypeScript, we can define the type of the key and value to be stored in the map. Syntax Following is the syntax create the map in TypeScript − let hashMap = new Map(); Parameters key_Type − It is ... Read More

What are Intersection Types in typescript?

Shubham Vora
Updated on 19-Dec-2022 15:38:09

1K+ Views

In this tutorial, we will learn what are Intersection types in TypeScript. With the help of TypeScript, we may mix various types to produce more comprehensive and effective use cases. You can learn how to build union and intersection types more effectively in TypeScript by knowing the design concept behind them. In TypeScript, a notion known as "Intersection Types" effectively enables us to combine different types. We can combine different type definitions and utilize existing ones by using intersection types. Although intersection and union types in Typescript are similar, they are used in very different ways. A type that combines ... Read More

How to implement class constants in TypesScript?

Shubham Vora
Updated on 19-Dec-2022 15:35:50

982 Views

In this tutorial, we will learn to implement class constants in TypeScript. Any application has shared values that need to be used by classes and modules. These numbers could be configuration parameters, environment settings, error codes, or status indications. Using constants to hold those values rather than hard-coding magic strings is recommended. Constant use makes a program manageable by preventing the repetition of values in different places. Object-oriented JavaScript is TypeScript. Classes, interfaces, and other object-oriented programming are supported by TypeScript. In terms of OOP, a class is a template for building objects. The object's data is contained within a ... Read More

How to push an element at the beginning of an array in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 15:33:20

9K+ Views

In this tutorial, we will learn to push the elements at the start of the array in TypeScript. There are different ways to push single or multiple elements at the start of the array in TypeScript. Here, we will learn three different methods to push array elements at the array's starting index. Using the Array.unshift() Method The Array.unshift() method of TypeScript allows us to add the element into the array at the beginning. Also, we can add multiple elements at the start of the array using the Array.unshift() method. Syntax Users can follow the syntax below to use the array.unshift() ... Read More

How to override multiple if-else conditions using a switch statement in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 15:30:38

1K+ Views

In this tutorial, we will learn to override the multiple if-else conditions using the switch case statement in TypeScript. The single if-else statement is used to execute the condition statement. If the condition becomes true, the statement of if block executes otherwise control fallbacks to the else block and executes its statement. In some cases, developers must execute the code blocks on different conditions. For that, they require to write multiple if-else statements of ladder of if-else statements. Here, we will convert that ladder of if-else statements to switch case statements. Converting If-else conditions to Switch Statement in TypeScript Here, ... Read More

How to invoke methods of a particular class in TypeScript?

Shubham Vora
Updated on 16-Dec-2022 17:27:58

5K+ Views

In this tutorial, users will learn to invoke a particular class method in TypeScript. The class is the basic concept of object-oriented programming. In simple terms, it contains member variables and methods we can access by creating the object of that particular class. So, class is the blueprint of the objects we create for that particular class. The class can contain the functions in TypeScript, and we can also call them to the method. So, users need to learn to access and invoke the method of the particular class. Invoking the method of a particular class in TypeScript In TypeScript, ... Read More

Advertisements