diff --git a/packages/react/src/components/ErrorBoundary/ErrorBoundary.js b/packages/react/src/components/ErrorBoundary/ErrorBoundary.tsx similarity index 74% rename from packages/react/src/components/ErrorBoundary/ErrorBoundary.js rename to packages/react/src/components/ErrorBoundary/ErrorBoundary.tsx index 15a2c224ab04..99194ca21c08 100644 --- a/packages/react/src/components/ErrorBoundary/ErrorBoundary.js +++ b/packages/react/src/components/ErrorBoundary/ErrorBoundary.tsx @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import React from 'react'; +import React, { ErrorInfo, ReactNode } from 'react'; import PropTypes from 'prop-types'; import { ErrorBoundaryContext } from './ErrorBoundaryContext'; @@ -26,29 +26,39 @@ import { ErrorBoundaryContext } from './ErrorBoundaryContext'; * Reference: * https://reactjs.org/docs/error-boundaries.html#introducing-error-boundaries */ -export default class ErrorBoundary extends React.Component { +interface ErrorBoundaryProps { + children?: ReactNode; + fallback?: ReactNode; +} + +interface ErrorBoundaryState { + hasError: boolean; +} + +export default class ErrorBoundary extends React.Component { static propTypes = { children: PropTypes.node, fallback: PropTypes.node, }; static contextType = ErrorBoundaryContext; + context!: React.ContextType; - static getDerivedStateFromError() { + static getDerivedStateFromError(): ErrorBoundaryState { return { hasError: true, }; } - state = { + state: ErrorBoundaryState = { hasError: false, }; - componentDidCatch(error, info) { + componentDidCatch(error: Error, info: ErrorInfo) { this.context.log(error, info); } - componentDidUpdate(prevProps) { + componentDidUpdate(prevProps: ErrorBoundaryProps) { if (prevProps.children !== this.props.children) { this.setState({ hasError: false }); } diff --git a/packages/react/src/components/ErrorBoundary/ErrorBoundaryContext.js b/packages/react/src/components/ErrorBoundary/ErrorBoundaryContext.ts similarity index 52% rename from packages/react/src/components/ErrorBoundary/ErrorBoundaryContext.js rename to packages/react/src/components/ErrorBoundary/ErrorBoundaryContext.ts index 6e4e4f3e2629..7142e15cd28e 100644 --- a/packages/react/src/components/ErrorBoundary/ErrorBoundaryContext.js +++ b/packages/react/src/components/ErrorBoundary/ErrorBoundaryContext.ts @@ -5,9 +5,13 @@ * LICENSE file in the root directory of this source tree. */ -import { createContext } from 'react'; +import { ErrorInfo, createContext } from 'react'; -export const ErrorBoundaryContext = createContext({ +export interface ErrorBoundaryContextType { + log: (error: Error, errorInfo: ErrorInfo) => void; +} + +export const ErrorBoundaryContext = createContext({ log(error, info) { console.log(info.componentStack); }, diff --git a/packages/react/src/components/ErrorBoundary/index.js b/packages/react/src/components/ErrorBoundary/index.ts similarity index 100% rename from packages/react/src/components/ErrorBoundary/index.js rename to packages/react/src/components/ErrorBoundary/index.ts