I would like to know whether this section of code is correct or there is a different way of defining it.
Instead of using error!, is there any way. I would like to know what error! actually means as unable to understand.
The code you have provided is correct if your expected behaviour is
errors
array.errors
array contains at least one error as you are returning an ErrorResult
rather than ErrorResult | undefined
or it's other equivalent.You can re-write it as
// Typescripts infers the type of var to be `ErrorResult | undefined`
let error = errors.find(element => element.error);
// ! tells the compiler that you know error is not `undefined`
return error!;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With