Skip to content

Commit 21f49bd

Browse files
committed
WIP
1 parent 35aadb9 commit 21f49bd

14 files changed

Lines changed: 308 additions & 7 deletions

notes/FUTURE.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
## Things to add
22

3-
Generic components
4-
Higher order components
5-
Typing forwardRef
3+
Function overloads in custom hooks?
4+
function overloads in components?
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
export const useId = (defaultId: string): string => {
5+
const [id] = useState<string>(defaultId);
6+
7+
return id;
8+
};
9+
10+
type tests = [Expect<Equal<typeof useId, (defaultId: string) => string>>];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
export const useStateAsObject = (initial: any) => {
5+
const [value, set] = useState(initial);
6+
7+
return {
8+
value,
9+
set,
10+
};
11+
};
12+
13+
const example = useStateAsObject({ name: "Matt" });
14+
15+
type ExampleTests = [
16+
Expect<Equal<typeof example.value, { name: string }>>,
17+
Expect<
18+
Equal<
19+
typeof example.set,
20+
React.Dispatch<React.SetStateAction<{ name: string }>>
21+
>
22+
>,
23+
];
24+
25+
const num = useStateAsObject(2);
26+
27+
type NumTests = [
28+
Expect<Equal<typeof num.value, number>>,
29+
Expect<Equal<typeof num.set, React.Dispatch<React.SetStateAction<number>>>>,
30+
];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
export const useStateAsObject = <T>(initial: T) => {
5+
const [value, set] = useState(initial);
6+
7+
return {
8+
value,
9+
set,
10+
};
11+
};
12+
13+
const example = useStateAsObject({ name: "Matt" });
14+
15+
type ExampleTests = [
16+
Expect<Equal<typeof example.value, { name: string }>>,
17+
Expect<
18+
Equal<
19+
typeof example.set,
20+
React.Dispatch<React.SetStateAction<{ name: string }>>
21+
>
22+
>,
23+
];
24+
25+
const num = useStateAsObject(2);
26+
27+
type NumTests = [
28+
Expect<Equal<typeof num.value, number>>,
29+
Expect<Equal<typeof num.set, React.Dispatch<React.SetStateAction<number>>>>,
30+
];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
export const useStateAsObject = <T>(
5+
initial: T,
6+
): {
7+
value: T;
8+
set: React.Dispatch<React.SetStateAction<T>>;
9+
} => {
10+
const [value, set] = useState(initial);
11+
12+
return {
13+
value,
14+
set,
15+
};
16+
};
17+
18+
const example = useStateAsObject({ name: "Matt" });
19+
20+
type ExampleTests = [
21+
Expect<Equal<typeof example.value, { name: string }>>,
22+
Expect<
23+
Equal<
24+
typeof example.set,
25+
React.Dispatch<React.SetStateAction<{ name: string }>>
26+
>
27+
>,
28+
];
29+
30+
const num = useStateAsObject(2);
31+
32+
type NumTests = [
33+
Expect<Equal<typeof num.value, number>>,
34+
Expect<Equal<typeof num.set, React.Dispatch<React.SetStateAction<number>>>>,
35+
];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
type UseStateAsObjectReturn<T> = {
5+
value: T;
6+
set: React.Dispatch<React.SetStateAction<T>>;
7+
};
8+
9+
export const useStateAsObject = <T>(initial: T): UseStateAsObjectReturn<T> => {
10+
const [value, set] = useState(initial);
11+
12+
return {
13+
value,
14+
set,
15+
};
16+
};
17+
18+
const example = useStateAsObject({ name: "Matt" });
19+
20+
type ExampleTests = [
21+
Expect<Equal<typeof example.value, { name: string }>>,
22+
Expect<
23+
Equal<
24+
typeof example.set,
25+
React.Dispatch<React.SetStateAction<{ name: string }>>
26+
>
27+
>,
28+
];
29+
30+
const num = useStateAsObject(2);
31+
32+
type NumTests = [
33+
Expect<Equal<typeof num.value, number>>,
34+
Expect<Equal<typeof num.set, React.Dispatch<React.SetStateAction<number>>>>,
35+
];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
interface UseStateAsObjectReturn<T> {
5+
value: T;
6+
set: React.Dispatch<React.SetStateAction<T>>;
7+
}
8+
9+
export const useStateAsObject = <T>(initial: T): UseStateAsObjectReturn<T> => {
10+
const [value, set] = useState(initial);
11+
12+
return {
13+
value,
14+
set,
15+
};
16+
};
17+
18+
const example = useStateAsObject({ name: "Matt" });
19+
20+
type ExampleTests = [
21+
Expect<Equal<typeof example.value, { name: string }>>,
22+
Expect<
23+
Equal<
24+
typeof example.set,
25+
React.Dispatch<React.SetStateAction<{ name: string }>>
26+
>
27+
>,
28+
];
29+
30+
const num = useStateAsObject(2);
31+
32+
type NumTests = [
33+
Expect<Equal<typeof num.value, number>>,
34+
Expect<Equal<typeof num.set, React.Dispatch<React.SetStateAction<number>>>>,
35+
];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState } from "react";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
export const useStateAsObject = <T>(initial: T) => {
5+
const [value, set] = useState<T>(initial);
6+
7+
return {
8+
value,
9+
set,
10+
};
11+
};
12+
13+
const example = useStateAsObject({ name: "Matt" });
14+
15+
type ExampleTests = [
16+
Expect<Equal<typeof example.value, { name: string }>>,
17+
Expect<
18+
Equal<
19+
typeof example.set,
20+
React.Dispatch<React.SetStateAction<{ name: string }>>
21+
>
22+
>,
23+
];
24+
25+
const num = useStateAsObject(2);
26+
27+
type NumTests = [
28+
Expect<Equal<typeof num.value, number>>,
29+
Expect<Equal<typeof num.set, React.Dispatch<React.SetStateAction<number>>>>,
30+
];
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { it } from "vitest";
2+
import { Equal, Expect } from "../helpers/type-utils";
3+
4+
export const useLocalStorage = (prefix: string) => {
5+
return {
6+
get: (key: string) => {
7+
return JSON.parse(window.localStorage.getItem(prefix + key) || "null");
8+
},
9+
set: (key: string, value: any) => {
10+
window.localStorage.setItem(prefix + key, JSON.stringify(value));
11+
},
12+
};
13+
};
14+
15+
const user = useLocalStorage<{ name: string }>("user");
16+
17+
it("Should let you set and get values", () => {
18+
user.set("matt", { name: "Matt" });
19+
20+
const mattUser = user.get("matt");
21+
22+
type tests = [Expect<Equal<typeof mattUser, { name: string } | null>>];
23+
});
24+
25+
it("Should not let you set a value that is not the same type as the type argument passed", () => {
26+
user.set(
27+
"something",
28+
// @ts-expect-error
29+
{},
30+
);
31+
});

src/03-custom-hooks/22-generic-localstorage-hook.problem.tsx

Whitespace-only changes.

0 commit comments

Comments
 (0)