Skip to content

fix(types): ensure forwardRef overrides the default ref type #4787

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compat/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ declare namespace React {

export function forwardRef<R, P = {}>(
fn: ForwardRefRenderFunction<R, P>
): preact.FunctionalComponent<PropsWithoutRef<P> & { ref?: preact.Ref<R> }>;
): preact.FunctionalComponent<PropsWithoutRef<P>, R>;

export type PropsWithoutRef<P> = Omit<P, 'ref'>;

Expand Down
6 changes: 3 additions & 3 deletions src/index-5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ export type ComponentProps<
? JSXInternal.IntrinsicElements[C]
: {};

export interface FunctionComponent<P = {}> {
(props: RenderableProps<P>, context?: any): VNode | null;
export interface FunctionComponent<P = {}, RefType = any> {
(props: RenderableProps<P, RefType>, context?: any): VNode | null;
displayName?: string;
defaultProps?: Partial<P> | undefined;
}
export interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}
export interface FunctionalComponent<P = {}, RefType = any> extends FunctionComponent<P, RefType> {}

export interface ComponentClass<P = {}, S = {}> {
new (props: P, context?: any): Component<P, S>;
Expand Down
6 changes: 3 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ export type ComponentProps<
? JSXInternal.IntrinsicElements[C]
: {};

export interface FunctionComponent<P = {}> {
(props: RenderableProps<P>, context?: any): ComponentChildren;
export interface FunctionComponent<P = {}, RefType = any> {
(props: RenderableProps<P, RefType>, context?: any): ComponentChildren;
Comment on lines +89 to +90
Copy link
Member

@rschristian rschristian Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct solution, React's FunctionComponent type does not take a second generic param so we shouldn't either.

It looks like the return type of forwardRef should be altered instead.

Edit: We do also have TS tests, in both core & compat, adding this case to one or both of those tests would be appreciated.

displayName?: string;
defaultProps?: Partial<P> | undefined;
}
export interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}
export interface FunctionalComponent<P = {}, RefType = any> extends FunctionComponent<P, RefType> {}

export interface ComponentClass<P = {}, S = {}> {
new (props: P, context?: any): Component<P, S>;
Expand Down