What is Type Assertion in TypeScript

In TypeScript, Type Assertions allow developers to explicitly specify the type of a variable to the TypeScript compiler.  When the compiler cannot infer the type of a variable and the developer is more sure about the type, they can use a type assertion. Type assertions are a compile-time construct that does not result in runtimeContinueContinue reading “What is Type Assertion in TypeScript”

What are Discriminated Unions Patterns in TypeScript?

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?”

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”

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?”

Video – Partial Type in TypeScript

This video explains Partial Type in TypeScript. interface IProduct { name: string; price: number; } function SaveProduct(product: Partial<IProduct>) { console.log(product.name); console.log(product.price); } SaveProduct({ name: “Laptop”, price: 1000 }); SaveProduct({ name: “Mobile”, price: 500 }); SaveProduct({ name: “Tablet”}); https://www.youtube.com/watch?v=q5wE_QjBtVU

Interface Segregation Principle in TypeScript

The interface Segregation Principle is one of the fundamental principles of the SOLID principle. It says to keep the interface as small as possible. In the words of Uncle Bob, keep interfaces small so that classes don’t end up depending on things they don’t need. A client ( class) should not be forced to implementContinueContinue reading “Interface Segregation Principle in TypeScript”

Make all properties optional using Partial in TypeScript

The utility type Partial<Type> makes all properties of a type optional. Using a scenario, let us try to understand Partial<T> in TypeScript. So, you have a type called Product, that is created as below:   export interface Product {   Id: string;   Title: string;   Price: number;   inStock: boolean;   Quantity: number; } And there is a function to update the price of a product as shownContinueContinue reading “Make all properties optional using Partial in TypeScript”