Skip to content

Commit 952cc4e

Browse files
committed
P/S'd 55
1 parent 591fe30 commit 952cc4e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ReactNode } from "react";
2+
3+
/**
4+
* In this example we have a Select component. Through some magic, we're
5+
* attempting to strongly type the children of the Select component so
6+
* that you can only pass 'Option' elements to it.
7+
*
8+
* 1. Try to understand the type of OptionType. What's the __brand property
9+
* for?
10+
*
11+
* 2. There's an error happening at <Option /> below. Why is that?
12+
*
13+
* 3. Try changing <Option /> to {Option()}. This appears to work. Why?
14+
* And why is this NOT a good idea?
15+
*
16+
* 4. Is what we're attempting to do even possible?
17+
*/
18+
19+
type OptionType = {
20+
__brand: "OPTION_TYPE";
21+
} & ReactNode;
22+
23+
const Option = () => {
24+
return (<option></option>) as OptionType;
25+
};
26+
27+
const Select = (props: { children: OptionType }) => {
28+
return <select>{props.children}</select>;
29+
};
30+
31+
<Select>
32+
<Option />
33+
</Select>;

0 commit comments

Comments
 (0)