Recently, I came across a pattern named Discriminated Unions Types in TypeScript. In this blog post, let us learn about that. Discriminated Unions are a pattern where each type in the union has a common property. In TypeScript, a union type allows a variable to hold one of several specified types. A straightforward example of aContinueContinue reading “What are Discriminated Unions Patterns in TypeScript?”
Author Archives: Dhananjay Kumar
What is Narrowing in TypeScript?
To understand narrowing, let’s begin with a problem scenario. Suppose you’re working with a custom type called myType, which is defined as a union of string and null. type mytype = string | null; let a: mytype = “FOO”; console.log(a!.toLowerCase()); // foo The code above should print “foo” in lowercase. Now, if you setContinueContinue reading “What is Narrowing in TypeScript?”
How to use TypeScript Custom Predicate for Narrowing
In TypeScript, narrowing refers to refining the type of a variable within a specific block of code. It allows TypeScript to infer a more particular type from a broader type based on runtime checks or control flow. Narrowing is used for better type safety, ensuring that only properties or methods on a type can be accessed. ItContinueContinue reading “How to use TypeScript Custom Predicate for Narrowing”
Getting Started with the Resource API in Angular- Video
Angular 19 will introduce a new reactive API called Resource API. The primary purpose of the Resource API is to load the resources, such as: Fetch data from the API Update data locally Asynchronously load local resource It should not be used for mutation such as POST operations. It should mainly be used to loadContinueContinue reading “Getting Started with the Resource API in Angular- Video”
Implementing a Global Error Handler in Angular: A Step-by-Step Guide
In an Angular application, errors typically fall into two categories: Errors related to HTTP operations Errors resulting from invalid application operations, like attempting to divide by zero This blog post delves into incorporating a global error handler in an Angular application to manage runtime errors throughout the application effectively. When working with Angular applications, it’s common toContinueContinue reading “Implementing a Global Error Handler in Angular: A Step-by-Step Guide”
Key announcements from the Angular Team about Angular and its future at the ngPoland keynote
On November 6, 2024, I attended the Angular Team’s keynote at ng-Poland, Europe’s largest Angular conference. The keynote was presented by Mark Techson, Alex Rickabaugh, and Pawel Kozlowski from the Angular team. Here are some key features I gathered from the keynote announcement. These enhancements and additions are impressive and will significantly advance the AngularContinueContinue reading “Key announcements from the Angular Team about Angular and its future at the ngPoland keynote”
Which is more suitable to use, isNaN or Number.isNaN() in JavaScript
In JavaScript, NaN is a valid value of the number type. console.log(typeof NaN); // number When prompted to verify if a value matches NaN or not, you’ll encounter an irony: NaN doesn’t equate to itself. Therefore, standard equality comparisons are ineffective for checking NaN values. if(NaN === NaN){ console.log(“NaN is equal to NaN”); } elseContinueContinue reading “Which is more suitable to use, isNaN or Number.isNaN() in JavaScript”
Do you know about TypeScript keyof operator?
In TypeScript, the keyof operator creates a union type of all the known public property names of the given type. To understand the actual use case of the keyof operator, let us consider the below example. There is a type created with an interface, interface IProduct { id: number; name: string; price: number; stock: number;ContinueContinue reading “Do you know about TypeScript keyof operator?”
Choosing Between for..in Loops and Object.values() in JavaScript
To determine whether to use a for…in loop or Object.values, consider a scenario with two objects const Product = { name:”product1″, price:100, } const ProductInvoice = { invoiceno : “invoice1”, customer : “customer1”, } These objects are linked to each other in the prototype chain. Object.setPrototypeOf(ProductInvoice, Product); Now, you iterate and list the values withContinueContinue reading “Choosing Between for..in Loops and Object.values() in JavaScript”
Object.values to iterate JavaScript object properties
In JavaScript, you can display the values of all public enumerable properties of an object by using Object.values(). For instance, if you have an object like the one depicted below: const Product = { name:”p1″, price:100, } You can output the values of all properties by utilizing Object.values() like this: let p = Object.values(Product); console.log(p);ContinueContinue reading “Object.values to iterate JavaScript object properties”