How to Check if a String Is Empty in TypeScript

Check if a string is empty in TypeScript: Command Prompt output of isNullOrEmpty and isNullOrWhitespace functions on empty, blank, null and undefined values

To check if a string is empty in TypeScript, compare it with an empty string: str === “” (or str.length === 0). If the value can also be null or undefined, use str == null || str === “”, and add trim() when a string of spaces should count as empty. This guide covers each … Read more >>

Check If a Key Exists in a TypeScript Object

Check If a Key Exists in a TypeScript Object

While working on a TypeScript project, you receive data from an API as an object. Before using any property from that object, you want to make sure the key actually exists to avoid errors. For example, you want to display the user’s email, but you’re not sure if the email key is present in the … Read more >>

How to Check if an Object Is Empty in TypeScript

Check if an object is empty in TypeScript: Command Prompt output of an isEmptyObject type guard tested with objects, null, arrays, Date and Map

To check if an object is empty in TypeScript, count its own keys: Object.keys(obj).length === 0 is true for {} and false as soon as the object has a property. In real code you also need to handle null and undefined, and decide what to do with arrays, dates and class instances. This guide builds … Read more >>

How to Sort a Number Array in Descending Order using TypeScript?

Sorting Numbers in Descending Order in TypeScript

Recently, one of my team members asked me to sort a numeric array in descending order. In this tutorial, I will explain how to sort a number array in descending order using TypeScript, using different methods and examples. Understanding the sort() Method TypeScript, being a superset of JavaScript, inherits the sort() method for arrays. By default, the … Read more >>

How to Sort an Array by Date in TypeScript?

In this tutorial, I will explain how to sort an array by date in TypeScript. Sorting arrays by date is a common task in web development, especially when dealing with data such as event schedules, appointment bookings, or historical records. I will cover both ascending and descending order sorting an array in TypeScript and include … Read more >>

Check If Object Is Undefined in TypeScript

Check if Object is Undefined Using TypeScript

You may have faced some issues where your code breaks because an object is undefined. For example, you’re trying to access a property of an object, but that object was never assigned a value. In such cases, checking if the object is undefined helps prevent runtime errors and keeps your code safe. In this tutorial, … Read more >>

Define and Use an Empty Object Type in TypeScript

Define and Use an Empty Object Type in TypeScript

While building a TypeScript function, you might want to accept an object as a parameter, but you don’t care what properties it has; you want to make sure it’s some object and not a string, number, or null. For example, you are writing a logging function that accepts any object and prints it to the … Read more >>

How to Sort an Array of Objects by Property in TypeScript

Sort an array of objects by property in TypeScript: Command Prompt output of tsc and node sorting employees by age and by name

To sort an array of objects by property in TypeScript, pass a compare function to sort(): arr.sort((a, b) => a.age – b.age) for a number property and arr.sort((a, b) => a.name.localeCompare(b.name)) for a string property. Swap a and b for descending order. This guide covers the TypeScript array sort for numbers, strings and dates, sorting … Read more >>

How to Unzip a File in Python?

Unzip a File in Python

I faced an issue recently when I needed to extract a large dataset from a zip file as a part of my project for one of my clients. In this tutorial, I will explain how to unzip a file in Python. I researched this topic and I will share my findings in this post along … Read more >>

How to Read Tab-Delimited Files in Python?

Read Tab-Delimited Files in Python

In this tutorial, I will explain how to read tab-delimited files in Python. Tab-delimited files, commonly known as TSV (Tab-Separated Values) files, are a simple text format for storing data in a tabular structure. Recently someone asked me how to read tab-delimited files which made me explore more about this topic and I will share … Read more >>