Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit 4425cf4

Browse files
authored
Generics article 5 (#6)
* Add sitemap * Up to step 7 of generics article
1 parent f7daccd commit 4425cf4

File tree

27 files changed

+244
-74
lines changed

27 files changed

+244
-74
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"files": "snippets/snippets/**/*.ts",
77
"options": {
8-
"printWidth": 49
8+
"printWidth": 48
99
}
1010
}
1111
]

public/sitemap.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
3+
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
4+
xmlns:xhtml="http://www.w3.org/1999/xhtml"
5+
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
6+
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
7+
<url>
8+
<loc>https://ts.chibicode.com</loc>
9+
</url>
10+
<url>
11+
<loc>https://ts.chibicode.com/generics</loc>
12+
</url>
13+
</urlset>

snippets/snippets/generics/bfka.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// We want to modify createState() to support
1+
// We want to modify makeState() to support
22
// creating two different states:
33

44
// One that only allows numbers, and…
5-
const numState = createState()
5+
const numState = makeState()
66
numState.setState(1)
77
console.log(numState.getState()) // 1
88

99
// The other that only allows strings.
10-
const strState = createState()
10+
const strState = makeState()
1111
strState.setState('foo')
1212
console.log(strState.getState()) // foo

snippets/snippets/generics/brze.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function createState<S>() {
1+
function makeState<S>() {
22
let state: S
33

44
function getState() {

snippets/snippets/generics/cbeq.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getState, setState } = createState()
1+
const { getState, setState } = makeState()
22

33
setState(1)
44
console.log(getState())

snippets/snippets/generics/cupt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function createState() {
1+
function makeState() {
22
let state: number
33

44
function getState() {

snippets/snippets/generics/defo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function createState<S>() {
1+
function makeState<S>() {
22
let state: S
33

44
function getState() {
@@ -12,10 +12,10 @@ function createState<S>() {
1212
return { getState, setState }
1313
}
1414

15-
const numState = createState<number>()
15+
const numState = makeState<number>()
1616
numState.setState(1)
1717
console.log(numState.getState())
1818

19-
const strState = createState<string>()
19+
const strState = makeState<string>()
2020
strState.setState('foo')
2121
console.log(strState.getState())

snippets/snippets/generics/dngl.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function makeState<
2+
S extends number | string
3+
>() {
4+
let state: S
5+
6+
function getState() {
7+
return state
8+
}
9+
10+
function setState(x: S) {
11+
state = x
12+
}
13+
14+
return { getState, setState }
15+
}
16+
17+
// What happens if we now pass boolean to S?
18+
const boolState = makeState<boolean>()

snippets/snippets/generics/gjgg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Creates a number-only state
2-
const numState = createState<number>()
2+
const numState = makeState<number>()
33
numState.setState(1)
44
console.log(numState.getState())
55

snippets/snippets/generics/gkgi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function createState() {
1+
function makeState() {
22
// Change to string
33
let state: string
44

0 commit comments

Comments
 (0)