Catching Common Errors with @ts-check
What you'll learn: How @ts-check acts as a safety net by catching typos, wrong argument counts, undefined variables, and null reference errors before runtime.
The Power of Static Analysis
Once you enable // @ts-check at the top of a JavaScript file, TypeScript's checker runs behind the scenes, analyzing your code without executing it. It catches mistakes that would otherwise only surface when your code runs—sometimes in production.
Common Errors It Catches
Typos in Property Names
If you type user.nmae instead of user.name, @ts-check will flag it immediately based on the inferred shape of user.
Wrong Argument Counts
Call a function with too many or too few arguments? @ts-check compares against the function's signature and warns you. If calculateTotal(price, tax) expects two parameters but you call it with one, you'll know instantly.
Undefined Variables
Reference a variable that doesn't exist? Unlike plain JavaScript (which throws at runtime), @ts-check highlights it during editing. No more ReferenceError: x is not defined surprises.
Null Reference Errors
The classic "Cannot read property of null" error gets caught early. If a variable might be null or undefined, @ts-check forces you to handle that case before accessing properties or methods.
Think of It as a Spell Checker
Just as a spell checker catches "teh" before you send an email, @ts-check catches code typos and logical mistakes before you ship code. It doesn't change how your code runs—it just makes sure the code you think you wrote matches what you actually wrote.
Key Takeaway: @ts-check transforms common runtime errors into immediate editor feedback, catching typos, incorrect function calls, undefined references, and null-access bugs during development rather than in production.