TypeScript Narrowing Assignments Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report TypeScript narrowing assignments is a type of type inference mechanism that occurs when TypeScript examines the right side of an assignment and uses that information to narrow down the type of the variable or property on the left side of the assignment.Syntaxlet value: string | number = valueOftheVariable;Parametersvalue is the name of the variablestring|number is the type associated with the variablevalueOftheVariable is the actual value of the variable that needs to be assignedExample 1: Assigning Different TypesIn this example, we first assign a string to a variable and then a number. TypeScript allows this because the declared type of x is string | number. Assignability is always checked against the declared type. If we assigned a boolean to x, it would result in an error since that wasn’t part of the declared type. JavaScript let x: string | number = "Hello"; console.log(x); // Output: Hello x = 42; console.log(x); // Output: 42 // x = true; // Error: Type 'boolean' is not assignable to type 'string | number'. Output:Hello42Example 2: Narrowing in Function ParametersIn this example, we define a function printLengthOrValue that takes a parameter input with a type of string | number. Inside the function, we use the typeof operator to check the type of input. TypeScript narrows down the type of input within the conditional blocks. JavaScript function printLengthOrValue(input: string | number): void { if (typeof input === "string") { console.log(`Length of string: ${input.length}`); } else { console.log(`Value of number: ${input}`); } } printLengthOrValue("Hello"); // Output: Length of string: 5 printLengthOrValue(42); // Output: Value of number: 42 Output:Length of string: 5Value of number: 42ConclusionIn this article, we explored TypeScript narrowing assignments and their syntax. Narrowing assignments occur when TypeScript examines the right side of an assignment to narrow down the type of the variable or property on the left side. This type inference mechanism allows TypeScript to provide more accurate type information and perform type checking based on the actual values assigned, leading to safer and more robust code. Comment More infoAdvertise with us Next Article TypeScript Equality Narrowing Type A abhiisaxena09 Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Similar Reads TypeScript Equality Narrowing Type In this article, we are going to learn about Equality narrowing Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, "equality narrowing" refers to the process of narrowing the type of a variable based on equality checks 4 min read TypeScript Equality Narrowing Type In this article, we are going to learn about Equality narrowing Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, "equality narrowing" refers to the process of narrowing the type of a variable based on equality checks 4 min read How to implement Type narrowing in TypeScript? Type narrowing in TypeScript refers to refining the type of a variable within a conditional block based on runtime checks. This is achieved through techniques like typeof guards, instance checks, or property existence checks, enabling more precise typing and avoiding type errors in subsequent code e 2 min read How to implement Type narrowing in TypeScript? Type narrowing in TypeScript refers to refining the type of a variable within a conditional block based on runtime checks. This is achieved through techniques like typeof guards, instance checks, or property existence checks, enabling more precise typing and avoiding type errors in subsequent code e 2 min read TypeScript in operator narrowing Type In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na 3 min read TypeScript in operator narrowing Type In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na 3 min read Like