Skip to content

Commit 9a8df08

Browse files
committed
WIP
1 parent 490ca0b commit 9a8df08

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/08-advanced-patterns/72.5-as-prop-with-forward-ref.problem.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
ComponentPropsWithoutRef,
3-
ElementType,
43
JSXElementConstructor,
54
forwardRef,
65
} from "react";
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
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";
102
import { Equal, Expect } from "../helpers/type-utils";
113

124
type FixedForwardRef = <T, P = {}>(
@@ -15,16 +7,11 @@ type FixedForwardRef = <T, P = {}>(
157

168
const 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+
6461
Link({
6562
as: Custom,
6663
thisIsRequired: true,
64+
ref: ref,
6765
});
6866

6967
Link({
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

Comments
 (0)