What if your code could warn you about mistakes before you even run it?
How assignment compatibility is checked in Typescript - Why You Should Know This
Imagine you have a box labeled 'fruits' and you want to put apples, oranges, and bananas inside. But what if you accidentally put a shoe in there? Without a clear way to check, you might not notice until later.
Manually checking if every item fits the box's label is slow and error-prone. You might forget to check, or mix up items, causing bugs that are hard to find.
TypeScript's assignment compatibility automatically checks if the value you assign fits the expected type. It acts like a smart label checker, preventing mistakes before they happen.
let fruitBox: any; fruitBox = 'apple'; fruitBox = 42; // Oops, no warning
let fruitBox: string; fruitBox = 'apple'; fruitBox = 42; // Error: Type 'number' is not assignable to type 'string'
This lets you catch mistakes early, making your code safer and easier to understand.
When building a shopping cart, assignment compatibility ensures you only add valid product objects, not random data, preventing crashes and bugs.
Manual checks are slow and risky.
Assignment compatibility automates type safety.
It helps catch errors before running the code.