Intermediate
TypeScript
Q80 / 100
What is the benefit of using "unknown" as the type for a caught error in a try/catch block, e.g. "catch (err: unknown)"?
Correct! Well done.
Incorrect.
The correct answer is A) It forces you to narrow or check the type of err before accessing its properties, avoiding unsafe assumptions about the error shape
A
Correct Answer
It forces you to narrow or check the type of err before accessing its properties, avoiding unsafe assumptions about the error shape
Explanation
Since TypeScript 4.4, catch clause variables can be typed as unknown, which is safer than any because it requires explicit narrowing (e.g. checking "instanceof Error") before use.
Progress
80/100