TypeScript Master (50 Qs)•Part 1: Basic & Foundational TypeScript (Junior / Entry-Level)
What is the difference between Type Assertions (`as`) and Type Casting?
senior LevelTypeScript
In Simple Words:
- Type Casting in languages like C# or Java converts data representations at runtime (e.g. converting float bytes to integer).
- Type Assertion (
as Type) in TypeScript is purely a compile-time instruction telling the compiler: "Trust me, I know the type better than you do." It does not convert or sanitize any runtime data! - Non-null Assertion (
obj!.prop): Asserts thatobjis neithernullnorundefined. If it actually isnullat runtime, it triggers a crash!