According to the TypeScript definition the type is any
, whereas the related method componentDidCatch() receives an error of type Error
. Why this inconsistency?
Typescript has a global Error
interface that you can use.
interface Error {
stack?: string;
}
so you can use
static getDerivedStateFromError(error: Error) {
// ...
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
// ...
}
the GetDerivedStateFromError
type is shown below and I think it's error type should not be of type any
, but also of type Error
type GetDerivedStateFromError<P, S> =
/**
* This lifecycle is invoked after an error has been thrown by a descendant component.
* It receives the error that was thrown as a parameter and should return a value to update state.
*
* Note: its presence prevents any of the deprecated lifecycle methods from being invoked
*/
(error: any) => Partial<S> | null;
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