Discuss the differences between Type Assertions vs. Type Casting in TypeScript.

Instruction: Provide definitions and examples to highlight the differences between Type Assertions and Type Casting.

Context: This question aims to clarify the candidate's understanding of Type Assertions and Type Casting, including when and how to use each.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

A TypeScript assertion tells the compiler to treat a value as a compatible type. It is erased and performs no conversion, validation or checked cast at runtime. The term casting is sometimes used loosely for an assertion, so I would clarify whether the caller means a static assertion or an actual JavaScript conversion.

```typescript const raw: unknown = "123"; const...

Related Questions