Skip to content

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

Open
aleclarson wants to merge 1 commit into
preactjs:mainfrom
aleclarson:fix/forwardref-props-type
Open

fix(types): ensure forwardRef overrides the default ref type#4787
aleclarson wants to merge 1 commit into
preactjs:mainfrom
aleclarson:fix/forwardref-props-type

Conversation

@aleclarson

@aleclarson aleclarson commented Jun 5, 2025

Copy link
Copy Markdown

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)

@github-actions

github-actions Bot commented Jun 5, 2025

Copy link
Copy Markdown

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

@rschristian rschristian Jun 6, 2025

Copy link
Copy Markdown
Member

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