Skip to content

Commit 7faa883

Browse files
committed
MOAR exercises
1 parent 793dcd7 commit 7faa883

18 files changed

Lines changed: 76 additions & 0 deletions

notes/FUTURE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
Function overloads in custom hooks?
44
function overloads in components?
5+
Omit, Pick, inferface extends with props
6+
Picking props from external libraries
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type AsProps = {
2+
[K in keyof JSX.IntrinsicElements]: {
3+
as: K;
4+
} & JSX.IntrinsicElements[K];
5+
}[keyof JSX.IntrinsicElements];
6+
7+
export const Component = (props: AsProps) => {
8+
const Comp = props.as;
9+
return <Comp {...(props as any)}></Comp>;
10+
};
11+
12+
const yeah = <Component as="a" href="awdawd"></Component>;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type AsProps = {
2+
[K in keyof JSX.IntrinsicElements]: {
3+
as: K;
4+
} & JSX.IntrinsicElements[K];
5+
}[keyof JSX.IntrinsicElements];
6+
7+
export const Component = (props: AsProps) => {
8+
const Comp = props.as;
9+
return <Comp {...(props as any)}></Comp>;
10+
};
11+
12+
const example1 = <Component as="a" href="awdawd"></Component>;
13+
14+
const example2 = (
15+
<Component
16+
as="div"
17+
// Property 'href' does not exist
18+
href="awdawd"
19+
></Component>
20+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const Component = <TAs extends keyof JSX.IntrinsicElements>(
2+
props: {
3+
as: TAs;
4+
} & JSX.IntrinsicElements[TAs],
5+
) => {
6+
const Comp = props.as as string;
7+
return <Comp {...(props as any)}></Comp>;
8+
};
9+
10+
const yeah = <Component as="input" value="awd"></Component>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
3+
export const Component = (props: { as: any }) => {
4+
const Comp = props.as;
5+
return <Comp {...(props as any)}></Comp>;
6+
};
7+
8+
const Link = (props: { href: string; children?: React.ReactNode }) => {
9+
return <a href={props.href}>{props.children}</a>;
10+
};
11+
12+
const yeah = <Component as={Link} href="awdawd"></Component>;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { ComponentProps, JSXElementConstructor } from "react";
2+
3+
export const Component = <
4+
T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
5+
>(
6+
props: {
7+
as: T;
8+
} & ComponentProps<T>,
9+
) => {
10+
const Comp = props.as;
11+
return <Comp {...(props as any)}></Comp>;
12+
};
13+
14+
const Link = (props: { href: string; children?: React.ReactNode }) => {
15+
return <a href={props.href}>{props.children}</a>;
16+
};
17+
18+
// It works!
19+
20+
<Component as={Link} href="awdawd"></Component>;
File renamed without changes.
File renamed without changes.

src/05-advanced/46-render-props-with-generics.problem.tsx renamed to src/06-external-libraries/48-react-hook-form.problem.tsx

File renamed without changes.

src/06-external-libraries/47-react-hook-form.problem.tsx renamed to src/06-external-libraries/49-react-query.problem.tsx

File renamed without changes.

0 commit comments

Comments
 (0)