Classes 9 exercises

Using Classes in TypeScript

The solution here is actually beautifully simple.

We can update handleCustomError to specify error as a code number, with an accompanying message string:

const handleCustomError = (error: {code: number; message: string}) => {

To demonstrate how it works now, let's cr

Loading solution


0:00 The solution here is actually beautifully simple. We can specify error as, I mean, we could specify as, let's say code number like this and maybe have a message on it string. If we pass something to it, so constCustomError is new CustomError. Let's set this up again, oh no, 401.

0:22 Then what we would get handleCustomError, we could just pass in this custom error and it would be happy. Because we specifically want to be able to take in custom errors with exactly that shape, then we can just pass in custom error like this. How cool is that?

0:40 We can actually use the name of the class as a type in Typescript. Classes have this funny property where they can actually cross boundaries a little bit and be used as either a type or a runtime value. Pretty cool.