Unverified Commit 76d7dc1e authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Fix `unstable_catchError` type inference for fallback props (#91485)

### Why?

TypeScript fails to infer the generic `P` from `Omit<P, 'children'>` in
the fallback function parameter.

```
Argument of type '(props: { title: string; }, { error, reset, unstable_retry }: ErrorInfo) => Element' is not assignable to parameter of type '(props: Omit<UserProps, "children">, errorInfo: ErrorInfo) => ReactNode'.
  Types of parameters 'props' and 'props' are incompatible.
    Property 'title' is missing in type 'Omit<UserProps, "children">' but required in type '{ title: string; }'.ts(2345)
catch-error-wrapper.tsx(7, 12): 'title' is declared here.
```
parent 9eed3027
// Pages Router only
import Error from './dist/api/error' import Error from './dist/api/error'
export * from './dist/api/error' export * from './dist/api/error'
export default Error export default Error
export { unstable_catchError } from './dist/api/error'
export type { ErrorInfo } from './dist/api/error'
...@@ -22,10 +22,10 @@ type CatchErrorProps<P extends UserProps> = { ...@@ -22,10 +22,10 @@ type CatchErrorProps<P extends UserProps> = {
pathname: string | null pathname: string | null
isPagesRouter: boolean isPagesRouter: boolean
fallback: React.ComponentType<{ fallback: React.ComponentType<{
props: Omit<P, 'children'> props: P
errorInfo: ErrorInfo errorInfo: ErrorInfo
}> }>
props: Omit<P, 'children'> props: P
children: React.ReactNode children: React.ReactNode
} }
...@@ -183,23 +183,18 @@ export function unstable_catchError<P extends UserProps>( ...@@ -183,23 +183,18 @@ export function unstable_catchError<P extends UserProps>(
fallback: ( fallback: (
// children is omitted by design as the error fallback component is the "fallback" // children is omitted by design as the error fallback component is the "fallback"
// for the children when an error occurs. // for the children when an error occurs.
props: Omit<P, 'children'>, props: P,
errorInfo: ErrorInfo errorInfo: ErrorInfo
) => React.ReactNode ) => React.ReactNode
): React.ComponentType<P> { ): React.ComponentType<P & { children?: React.ReactNode }> {
// Create Fallback component from the closure of `unstable_catchError`. // Create Fallback component from the closure of `unstable_catchError`.
const Fallback = ({ const Fallback = ({ props, errorInfo }: { props: P; errorInfo: ErrorInfo }) =>
props, fallback(props, errorInfo)
errorInfo,
}: {
props: Omit<P, 'children'>
errorInfo: ErrorInfo
}) => fallback(props, errorInfo)
// Rename to match the user component name for DevTools. // Rename to match the user component name for DevTools.
Fallback.displayName = fallback.name || 'CatchErrorFallback' Fallback.displayName = fallback.name || 'CatchErrorFallback'
return ({ children, ...props }: P) => { return ({ children, ...props }: P & { children?: React.ReactNode }) => {
// When we're rendering the missing params shell, this will return null. This // When we're rendering the missing params shell, this will return null. This
// is because we won't be rendering any not found boundaries or error // is because we won't be rendering any not found boundaries or error
// boundaries for the missing params shell. When this runs on the client // boundaries for the missing params shell. When this runs on the client
...@@ -212,7 +207,7 @@ export function unstable_catchError<P extends UserProps>( ...@@ -212,7 +207,7 @@ export function unstable_catchError<P extends UserProps>(
pathname={pathname} pathname={pathname}
isPagesRouter={isPagesRouter} isPagesRouter={isPagesRouter}
fallback={Fallback} fallback={Fallback}
props={props} props={props as P}
> >
{children} {children}
</CatchError> </CatchError>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment