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

Conversation

aleclarson
Copy link

@aleclarson aleclarson commented Jun 5, 2025

This fixes an issue with forwardRef that was leading to a type conflict, which was basically…

(Ref<HTMLDivElement> | undefined) // …from forwardRef's return type
  & (Ref<any> | undefined)        // …from the RenderableProps type

Try to assign the ref prop of a forwardRef component to a ref callback, its argument's type defaults to any no matter the component type.

Reproduction

/* @jsxImportSource preact */
import { forwardRef } from "preact/compat";

declare class Element {}

const Foo = forwardRef<Element>(() => {
	return <div />;
});

const element = <Foo ref={(node) => {}} />;
//                         ^? any (should be Element)

Copy link

github-actions bot commented Jun 5, 2025

📊 Tachometer Benchmark Results

Summary

A summary of the benchmark results will show here once they finish.

Results

The full results of your benchmarks will show here once they finish.

tachometer-reporter-action v2 for CI

Comment on lines +89 to +90
export interface FunctionComponent<P = {}, RefType = any> {
(props: RenderableProps<P, RefType>, context?: any): ComponentChildren;
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants