1- import {
2- ComponentProps ,
3- ComponentPropsWithRef ,
4- ComponentPropsWithoutRef ,
5- ElementType ,
6- JSXElementConstructor ,
7- forwardRef ,
8- useRef ,
9- } from "react" ;
1+ import { ComponentPropsWithRef , ElementType , forwardRef , useRef } from "react" ;
102import { Equal , Expect } from "../helpers/type-utils" ;
113
124type FixedForwardRef = < T , P = { } > (
@@ -15,16 +7,11 @@ type FixedForwardRef = <T, P = {}>(
157
168const fixedForwardRef = forwardRef as FixedForwardRef ;
179
18- type Constraint = keyof JSX . IntrinsicElements | JSXElementConstructor < any > ;
19-
20- type Props <
21- T extends Constraint ,
22- P = ComponentProps < Constraint extends T ? "a" : T > ,
23- > = P ;
24-
25- type Example = Props < "button" > ;
26-
27- function UnwrappedLink < T extends Constraint > ( props : { as ?: T } & Props < T > ) {
10+ function UnwrappedLink < T extends ElementType > (
11+ props : {
12+ as ?: T ;
13+ } & Omit < ComponentPropsWithRef < ElementType extends T ? "a" : T > , "as" > ,
14+ ) {
2815 const { as : Comp = "a" , ...rest } = props ;
2916 return < Comp { ...rest } > </ Comp > ;
3017}
@@ -38,9 +25,14 @@ const Link = fixedForwardRef(UnwrappedLink);
3825 } }
3926> </ Link > ;
4027
41- const Custom = ( props : { thisIsRequired : boolean } ) => {
42- return null ;
43- } ;
28+ const Custom = forwardRef (
29+ (
30+ props : { thisIsRequired : boolean } ,
31+ ref : React . ForwardedRef < HTMLAnchorElement > ,
32+ ) => {
33+ return < a ref = { ref } /> ;
34+ } ,
35+ ) ;
4436
4537< Link as = { Custom } thisIsRequired /> ;
4638
@@ -61,15 +53,27 @@ Link({
6153 ref,
6254} ) ;
6355
56+ Link ( {
57+ // @ts -expect-error
58+ ref : wrongRef ,
59+ } ) ;
60+
6461Link ( {
6562 as : Custom ,
6663 thisIsRequired : true ,
64+ ref : ref ,
6765} ) ;
6866
6967Link ( {
68+ as : Custom ,
69+ thisIsRequired : true ,
7070 // @ts -expect-error
7171 ref : wrongRef ,
7272} ) ;
7373
7474// @ts -expect-error: Property 'href' does not exist
7575< Link as = "div" href = "awdawd" > </ Link > ;
76+
77+ Link ( {
78+ as : "div" ,
79+ } ) ;
0 commit comments