Classes as Types and Values
In this exercise, we have a handleCustomError
function which takes in something with an error code
.
const handleCustomError = (error: unknown) => { console.error(error.code); type test = Expect<Equal<typeof error.code, number>>;};
We only want to handle this type of `Cu
Transcript
0:00 In this exercise, we have a handleCustomError function, which takes in something with an error code here. We really want to only be handling this type of CustomError, which is a class which extends error and adds a code property to it.
0:16 We could potentially create a new CustomError, which instantiates new CustomError, passing it to ('Oh noo', 401) for instance. This CustomError then has the type of CustomError, and you can use things like instantof CustomError. We can say if let's say (customError instanceof CustomError) then we're happy and we know that it's customized.
0:43 It's a cool pattern that we've not really explored before in total TypeScript. The thing I'm interested in is how do we type this error parameter? We want to know that the error has a code on it, and we also probably want to know that the error has a message on it. We want to make sure the thing we're passing in is a CustomError. Good luck.