What is the difference between as and angle bracket syntax for type assertions?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for TypeScript development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
TypeScript offers two syntaxes for type assertions that are functionally identical in most cases: Angle bracket syntax: let len = (<string>someValue).length; — the older form from early TypeScript. as syntax: let len = (someValue as string).length; — the modern, preferred form. The critical difference is in JSX/TSX files: angle bracket syntax conflicts with JSX element syntax (which also uses angle brackets), so it cannot be used in .tsx files. The as keyword is unambiguous and works everywhere. For this reason and for consistency, the TypeScript community has standardized on the as syntax. TypeScript's official documentation and most style guides recommend using as exclusively. Both syntaxes generate identical compiled JavaScript output (which is nothing — assertions are erased at compile time).
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex TypeScript answers easy to follow.