+
+ The nuances of exercise 3
+
+
+ This is a fairly open ended exercise. The problem is that a try catch block will always treat the error as type unknown, which isn't helpful and is prone to type errors. The simplest solution is to treat an error as any type, which is commonly done in the workplace. This is a valid approach to handling errors but it has the usual problems of the any type so trainees should be encouraged to handle errors with more nuance.
+
+ A slightly better approach is to use type coercion. For example return (error as Error).message but this is also flawed since the error type of a catch block is inherently unpredictable.
+
+ A more robust solution would be to confirm the type of error before utilising it. For example:
+
+ if (error instanceof Error) {
+ return error.message
+ }
+ else {
+ return error
+ }
+
+
+ There is no singular solution to this exercise so it is best to encourage trainees to see how far they can take error handling and TypeScript.
+
+
+