TypeScript · Senior · Question 34 of 75
When is the double assertion x as unknown as T justified?
- ✓Correct answer: Almost never: only for a deliberate workaround backed by a check
- 2Always, whenever the compiler complains about incompatible types
- 3It's the standard, safe way to cast JSON to a type
Explanation
A plain as is forbidden between unrelated types; via unknown you can assert anything - and be wrong. If you can't avoid it (e.g. in a low-level utility), hide it inside a function with a runtime check and tests. For JSON, validate with a schema.