Skip to content

Commit 50acd9b

Browse files
committed
Improved forwardRef with generics explainer
1 parent a080f24 commit 50acd9b

File tree

2 files changed

+46
-63
lines changed

2 files changed

+46
-63
lines changed

src/08-advanced-patterns/65-forward-ref-with-generics.explainer.3.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Equal, Expect } from "../helpers/type-utils";
2+
3+
type TableProps<T> = {
4+
data: T[];
5+
renderRow: (item: T) => React.ReactNode;
6+
};
7+
8+
export const Table = <T,>(props: TableProps<T>) => {
9+
return null;
10+
};
11+
12+
type FC<Props> = {
13+
(arg: Props): React.ReactNode;
14+
// Try uncommenting this - and it works!
15+
someOtherThing?: string;
16+
};
17+
18+
const removeInference = <Props,>(component: FC<Props>) => {
19+
return component;
20+
};
21+
22+
const TableWithoutInference = removeInference(Table);
23+
24+
const Parent = () => {
25+
return (
26+
<>
27+
<TableWithoutInference
28+
data={["123"]}
29+
renderRow={(row) => {
30+
// Without inference, this is 'unknown'
31+
type test = Expect<Equal<typeof row, string>>;
32+
return <div>123</div>;
33+
}}
34+
></TableWithoutInference>
35+
36+
<Table
37+
data={["123"]}
38+
renderRow={(row) => {
39+
// With inference, it's 'string'
40+
type test = Expect<Equal<typeof row, string>>;
41+
return <div>123</div>;
42+
}}
43+
></Table>
44+
</>
45+
);
46+
};

0 commit comments

Comments
 (0)