-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use React.FC
for PanelStack2 renderPanel
typing to fix compilation on newer @types/react
versions
#6521
Use React.FC
for PanelStack2 renderPanel
typing to fix compilation on newer @types/react
versions
#6521
Conversation
Follow `React.FC` return type for `renderPanel` typingsBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
Hmm, that's strange, I can see the type error in the sandbox repro you linked. But when I tried to repro it myself, I don't get that error: https://codesandbox.io/p/sandbox/react-18-panelstack2-types-debugging-pcdslz?file=%2Fsrc%2FCoreExample.tsx%3A64%2C11 There are a bunch of places in the codebase where we expect a return type of |
That's really surprising. Taking a look, I see there's 2 definitions of Counterintuitively, I think the {
"exports": {
".": {
"types@<=5.0": {
"default": "./ts5.0/index.d.ts"
},
"types": {
"default": "./index.d.ts"
}
}
// ...
}
// ...
} The playground in the link above is using the I checked locally and see the issue on TypeScript 5.1.6.
I think the
I think we still have a bit more time to decide the best next step; I wouldn't want us to rush replacing |
@@ -21,7 +21,7 @@ export interface Panel<P> { | |||
/** | |||
* The renderer for this panel. | |||
*/ | |||
renderPanel: (props: PanelProps<P>) => JSX.Element | null; | |||
renderPanel: (props: PanelProps<P>) => ReturnType<React.FC<PanelProps<P>>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could also just use React.FC
entirely.
renderPanel: (props: PanelProps<P>) => ReturnType<React.FC<PanelProps<P>>>; | |
renderPanel: React.FC<PanelProps<P>>; |
But that is a bit more strict than what we had before this PR since React.FC
has additional optional properties on it.
interface FunctionComponent<P = {}> {
(props: P, context?: any): ReactNode;
propTypes?: WeakValidationMap<P> | undefined;
contextTypes?: ValidationMap<any> | undefined;
defaultProps?: Partial<P> | undefined;
displayName?: string | undefined;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this out and it seems to work fine with both functions that are annotated as React.FC
and plain (un-annotated) ones which return any of the ReactNode
types: https://codesandbox.io/p/sandbox/react-18-panelstack2-types-debugging-typescript-v5-x-hz29zl?file=%2Fsrc%2FCoreExample.tsx%3A18%2C1
so I think we can go ahead with this suggested change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thank you for the help testing that React.FC<...>
works for those cases. PR should be updated for this now.
b9f25ad
to
e0b8098
Compare
React.FC
return type for renderPanel
typings to fix compilation on newer @types/react
versionsReact.FC
for PanelStack2 renderPanel
typing to fix compilation on newer @types/react
versions
Use `React.FC` for PanelStack2 `renderPanel` prop typingBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
Context
A semi-recent change in
@types/react
switchedReact.FunctionComponent
's return type fromReactElement
toReactNode
.Break
Under
@types/[email protected]
and@types/[email protected]
, the following minimal example from the docs passes TypeScript compilation.However, on
@types/[email protected]
, it fails with:Changes
This PR fixes compilation of panel stack components under newer versions of
@types/react
by followingReact.FC
's typings.Notes for Reviews
My initial attempt to fix this was to add
React.ReactNode
as another possiblerenderPanel
return type. While this works on newer versions of@types/react
, it breaks on older versions.