Skip to content

Commit d680265

Browse files
committed
Various fixes
1 parent dbafb32 commit d680265

12 files changed

+11
-12
lines changed

src/08-advanced-patterns/72-as-prop-with-default.problem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export const Link = <T extends ElementType>(
66
as: T;
77
} & ComponentPropsWithoutRef<T>,
88
) => {
9-
const { as: Comp, ...rest } = props;
10-
return <Comp {...rest}></Comp>;
9+
const Comp = props.as;
10+
return <Comp {...(props as any)}></Comp>;
1111
};
1212

1313
/**

src/08-advanced-patterns/72-as-prop-with-default.solution.2.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { ComponentPropsWithoutRef, ElementType, useRef } from "react";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4-
/**
5-
* This NEARLY works, but removes autocomplete for the 'as' prop.
6-
*/
74
export const Link = <T extends ElementType>(
85
props: {
96
as?: T;

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import {
22
ComponentPropsWithoutRef,
3-
JSXElementConstructor,
3+
ElementType,
44
forwardRef,
55
useRef,
66
} from "react";
77
import { Equal, Expect } from "../helpers/type-utils";
88

9-
function UnwrappedLink<
10-
T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
11-
>(
9+
export const UnwrappedLink = <T extends ElementType>(
1210
props: {
1311
as?: T;
14-
} & ComponentPropsWithoutRef<T>,
15-
) {
12+
} & ComponentPropsWithoutRef<ElementType extends T ? "a" : T>,
13+
) => {
1614
const { as: Comp = "a", ...rest } = props;
1715
return <Comp {...rest}></Comp>;
18-
}
16+
};
1917

2018
const Link = forwardRef(UnwrappedLink);
2119

src/08-advanced-patterns/72.5-as-prop-with-forward-ref.solution.tsx renamed to src/08-advanced-patterns/73-as-prop-with-forward-ref.solution.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { ComponentProps, ElementType, forwardRef, useRef } from "react";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4+
// Added fixedForwardRef from a previous exercise
5+
46
type FixedForwardRef = <T, P = {}>(
57
render: (props: P, ref: React.Ref<T>) => React.ReactNode,
68
) => (props: P & React.RefAttributes<T>) => React.ReactNode;
79

810
const fixedForwardRef = forwardRef as FixedForwardRef;
911

12+
// Added a DistributiveOmit type
13+
1014
type DistributiveOmit<T, TOmitted extends PropertyKey> = T extends any
1115
? Omit<T, TOmitted>
1216
: never;

0 commit comments

Comments
 (0)