Overview - Equality narrowing
What is it?
Equality narrowing is a TypeScript feature that helps the compiler understand the specific type of a variable after you compare it with another value. When you check if two values are equal or not, TypeScript can 'narrow' the possible types of the variable based on that check. This makes your code safer and clearer because TypeScript knows exactly what type you are working with in different parts of your code.
Why it matters
Without equality narrowing, TypeScript would treat variables as having all their possible types all the time, making it harder to catch mistakes before running the program. Equality narrowing lets TypeScript catch errors early by understanding which type a variable really has after a comparison. This helps prevent bugs and makes coding easier and more reliable.
Where it fits
Before learning equality narrowing, you should understand basic TypeScript types and union types. After mastering equality narrowing, you can learn about other type narrowing techniques like type guards, instanceof checks, and discriminated unions.