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" ;
10
2
import { Equal , Expect } from "../helpers/type-utils" ;
11
3
12
4
type FixedForwardRef = < T , P = { } > (
@@ -15,16 +7,11 @@ type FixedForwardRef = <T, P = {}>(
15
7
16
8
const fixedForwardRef = forwardRef as FixedForwardRef ;
17
9
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
+ ) {
28
15
const { as : Comp = "a" , ...rest } = props ;
29
16
return < Comp { ...rest } > </ Comp > ;
30
17
}
@@ -38,9 +25,14 @@ const Link = fixedForwardRef(UnwrappedLink);
38
25
} }
39
26
> </ Link > ;
40
27
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
+ ) ;
44
36
45
37
< Link as = { Custom } thisIsRequired /> ;
46
38
@@ -61,15 +53,27 @@ Link({
61
53
ref,
62
54
} ) ;
63
55
56
+ Link ( {
57
+ // @ts -expect-error
58
+ ref : wrongRef ,
59
+ } ) ;
60
+
64
61
Link ( {
65
62
as : Custom ,
66
63
thisIsRequired : true ,
64
+ ref : ref ,
67
65
} ) ;
68
66
69
67
Link ( {
68
+ as : Custom ,
69
+ thisIsRequired : true ,
70
70
// @ts -expect-error
71
71
ref : wrongRef ,
72
72
} ) ;
73
73
74
74
// @ts -expect-error: Property 'href' does not exist
75
75
< Link as = "div" href = "awdawd" > </ Link > ;
76
+
77
+ Link ( {
78
+ as : "div" ,
79
+ } ) ;
0 commit comments