From 22185e410e33ff30df52c4965598173ce992a656 Mon Sep 17 00:00:00 2001
From: Shu Uesugi
- First, I created a function called
- When you run Now that we got the basics down, here’s a challenge question:
Here’s what I mean:
- Our first
- How can we modify () {
+function makeState() {
let state: S
function getState() {
diff --git a/snippets/snippets/generics/cbeq.ts b/snippets/snippets/generics/cbeq.ts
index 9e8f346..bcc4f92 100644
--- a/snippets/snippets/generics/cbeq.ts
+++ b/snippets/snippets/generics/cbeq.ts
@@ -1,4 +1,4 @@
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState(1)
console.log(getState())
diff --git a/snippets/snippets/generics/cupt.ts b/snippets/snippets/generics/cupt.ts
index 29911e8..9507081 100644
--- a/snippets/snippets/generics/cupt.ts
+++ b/snippets/snippets/generics/cupt.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
let state: number
function getState() {
diff --git a/snippets/snippets/generics/defo.ts b/snippets/snippets/generics/defo.ts
index 233a224..625fd70 100644
--- a/snippets/snippets/generics/defo.ts
+++ b/snippets/snippets/generics/defo.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
let state: S
function getState() {
@@ -12,10 +12,10 @@ function createState() {
return { getState, setState }
}
-const numState = createState()
diff --git a/snippets/snippets/generics/nnyl.ts b/snippets/snippets/generics/nnyl.ts
index d5ecab4..11fa6b7 100644
--- a/snippets/snippets/generics/nnyl.ts
+++ b/snippets/snippets/generics/nnyl.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
let state: number
function getState() {
diff --git a/snippets/snippets/generics/osaa.ts b/snippets/snippets/generics/osaa.ts
index 0a64aad..08cec96 100644
--- a/snippets/snippets/generics/osaa.ts
+++ b/snippets/snippets/generics/osaa.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
// Change to string
let state: string
@@ -14,7 +14,7 @@ function createState() {
return { getState, setState }
}
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState('foo')
console.log(getState())
diff --git a/snippets/snippets/generics/qqic.ts b/snippets/snippets/generics/qqic.ts
index 77433b6..148d91f 100644
--- a/snippets/snippets/generics/qqic.ts
+++ b/snippets/snippets/generics/qqic.ts
@@ -1,5 +1,5 @@
// Doesn't work because the created state…
-const numAndStrState = createState()
+const numAndStrState = makeState()
// Supports both numbers…
numAndStrState.setState(1)
diff --git a/snippets/snippets/generics/rebo.ts b/snippets/snippets/generics/rebo.ts
index 7634715..4cda5c0 100644
--- a/snippets/snippets/generics/rebo.ts
+++ b/snippets/snippets/generics/rebo.ts
@@ -1,4 +1,4 @@
-// In the function definition of createState()
+// In the function definition of makeState()
let state: S // <- number
function setState(x: S /* <- number */) {
diff --git a/snippets/snippets/generics/stkh.ts b/snippets/snippets/generics/stkh.ts
index 1ee3c09..6925591 100644
--- a/snippets/snippets/generics/stkh.ts
+++ b/snippets/snippets/generics/stkh.ts
@@ -1,4 +1,4 @@
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
// What happens if we use a string instead?
setState('foo')
diff --git a/snippets/snippets/generics/udpv.ts b/snippets/snippets/generics/udpv.ts
index 5dfdf0c..ad83dcd 100644
--- a/snippets/snippets/generics/udpv.ts
+++ b/snippets/snippets/generics/udpv.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
let state: number
function getState() {
@@ -12,7 +12,7 @@ function createState() {
return { getState, setState }
}
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState(1)
console.log(getState())
diff --git a/snippets/snippets/generics/xeax.ts b/snippets/snippets/generics/xeax.ts
index 5632156..93bb86e 100644
--- a/snippets/snippets/generics/xeax.ts
+++ b/snippets/snippets/generics/xeax.ts
@@ -1,4 +1,4 @@
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState('foo')
console.log(getState())
diff --git a/snippets/snippets/generics/ystu.ts b/snippets/snippets/generics/ystu.ts
index 0f56219..d0df0d5 100644
--- a/snippets/snippets/generics/ystu.ts
+++ b/snippets/snippets/generics/ystu.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
let state: number | string
function getState() {
diff --git a/snippets/snippets/generics/zhql.ts b/snippets/snippets/generics/zhql.ts
index 6a6a2a2..f1a53cb 100644
--- a/snippets/snippets/generics/zhql.ts
+++ b/snippets/snippets/generics/zhql.ts
@@ -1,4 +1,4 @@
-function createState() {
+function makeState() {
let state: number
function getState() {
@@ -12,7 +12,7 @@ function createState() {
return { getState, setState }
}
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState('foo')
console.log(getState())
diff --git a/src/components/Emoji/ChickEgg.tsx b/src/components/Emoji/ChickEgg.tsx
new file mode 100644
index 0000000..663f501
--- /dev/null
+++ b/src/components/Emoji/ChickEgg.tsx
@@ -0,0 +1,26 @@
+import React from 'react'
+
+const SvgChickEgg = (props: React.SVGProps() {
+export const brze = `function makeState() {
let state: S
function getState() {
@@ -25,7 +25,7 @@ export const brze = `function createState() {
return { getState, setState }
}`
-export const cbeq = `const { getState, setState } = createState()
+export const cbeq = `const { getState, setState } = makeState()
setState(1)
console.log(getState())
@@ -33,7 +33,7 @@ console.log(getState())
setState(2)
console.log(getState())`
-export const cupt = `function createState() {
+export const cupt = `function makeState() {
let state: number
function getState() {
@@ -47,7 +47,7 @@ export const cupt = `function createState() {
return { getState, setState }
}`
-export const defo = `function createState() {
+export const defo = `function makeState() {
let state: S
function getState() {
@@ -61,22 +61,41 @@ export const defo = `function createState() {
return { getState, setState }
}
-const numState = createState()`
+
+export const nnyl = `function makeState() {
let state: number
function getState() {
@@ -123,7 +149,7 @@ export const nnyl = `function createState() {
return { getState, setState }
}`
-export const osaa = `function createState() {
+export const osaa = `function makeState() {
// Change to string
let state: string
@@ -139,13 +165,13 @@ export const osaa = `function createState() {
return { getState, setState }
}
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState('foo')
console.log(getState())`
export const qqic = `// Doesn't work because the created state…
-const numAndStrState = createState()
+const numAndStrState = makeState()
// Supports both numbers…
numAndStrState.setState(1)
@@ -158,20 +184,20 @@ console.log(numAndStrState.getState())
// This is NOT what we want. We want to create
// a number-only state, and a string-only state.`
-export const rebo = `// In the function definition of createState()
+export const rebo = `// In the function definition of makeState()
let state: S // <- number
function setState(x: S /* <- number */) {
state = x
}`
-export const stkh = `const { getState, setState } = createState()
+export const stkh = `const { getState, setState } = makeState()
// What happens if we use a string instead?
setState('foo')
console.log(getState())`
-export const udpv = `function createState() {
+export const udpv = `function makeState() {
let state: number
function getState() {
@@ -185,7 +211,7 @@ export const udpv = `function createState() {
return { getState, setState }
}
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState(1)
console.log(getState())
@@ -193,12 +219,12 @@ console.log(getState())
setState(2)
console.log(getState())`
-export const xeax = `const { getState, setState } = createState()
+export const xeax = `const { getState, setState } = makeState()
setState('foo')
console.log(getState())`
-export const ystu = `function createState() {
+export const ystu = `function makeState() {
let state: number | string
function getState() {
@@ -212,7 +238,7 @@ export const ystu = `function createState() {
return { getState, setState }
}`
-export const zhql = `function createState() {
+export const zhql = `function makeState() {
let state: number
function getState() {
@@ -226,7 +252,7 @@ export const zhql = `function createState() {
return { getState, setState }
}
-const { getState, setState } = createState()
+const { getState, setState } = makeState()
setState('foo')
console.log(getState())`
diff --git a/src/pages/generics.tsx b/src/pages/generics.tsx
index 34b5266..d8fdd8c 100644
--- a/src/pages/generics.tsx
+++ b/src/pages/generics.tsx
@@ -61,18 +61,18 @@ const Page = () => (
{
title: (
<>
- Let’s talk about createState()
+ Let’s talk about makeState()
>
),
content: (
<>
createState(){' '}
- below. We’ll use this function to talk about generics.
+ First, I created a function called makeState() below.
+ We’ll use this function to talk about generics.
createState(), it returns two functions:{' '}
+ When you run makeState(), it returns two functions:{' '}
getState() and setState(). You can use
these functions to set and get the variable called{' '}
state.
@@ -183,8 +183,8 @@ const Page = () => (
createState() such that, it can
- create two different states:
+ Can we modify makeState() such that, it can create
+ two different states:
createState() created number-only states,
- and our second createState() created string-only
- states. However, it couldn’t create both number-only states and
+ Our first makeState() created number-only states, and
+ our second makeState() created string-only states.
+ However, it couldn’t create both number-only states and
string-only states.
createState() to achieve our goal?
+ How can we modify makeState() to achieve our goal?
- Instead, we want createState() to support creating
- two different states: one that allows only numbers, and the other
- that allows only strings.
+ Instead, we want makeState() to support creating two
+ different states: one that allows only numbers, and the other that
+ allows only strings.
- createState() is now defined as{' '}
- createState<S>(). You can think of{' '}
+ makeState() is now defined as{' '}
+ makeState<S>(). You can think of{' '}
<S> as another argument that you have to pass
in when you call the function. But instead of passing a value, you
pass a type to it. It’s a type argument.
For example, you can pass the type number as{' '}
- S when you call createState():
+ S when you call makeState():
- Then, inside the function definition of createState()
- , S will be come number:
+ Then, inside the function definition of makeState(),{' '}
+ S will be come number:
@@ -276,7 +276,7 @@ const Page = () => (
And to create a string-only state, you can pass{' '}
number as S when you call{' '}
- createState():
+ makeState():
- That’s it! And we call createState<S>() a
+ That’s it! And we call makeState<S>() a
“generic function” because it’s flexible—you have a choice to make
it number-only or string-only. You know it’s a generic function if
it takes a type parameter when you call it.
createState<S>() is a generic function
+ makeState<S>() is a generic function
>
}
/>
@@ -303,7 +303,83 @@ const Page = () => (
},
{
title: <>But you can create a boolean state!>,
- content: <>>
+ content: (
+ <>
+
+ But wait a minute: If you pass{' '}
+ boolean to S, you can create a
+ boolean-only state.
+
+ Maybe we might NOT want this to be allowed.{' '}
+ Suppose that don’t want makeState() to be able to
+ create non-number or non-string states (like boolean
+ ). How can we make sure that this is the case?
+
makeState() from
+
+ The solution:{' '}
+ makeState(), you change the type
+ parameter <S> to{' '}
+ <S extends number | string>
+
+ By doing this, when you call makeState(), you’d only
+ be able to pass number, string, or any
+ other type that extends either number or{' '}
+ string into S.
+
+ Let’s see what happens now when you try to pass{' '}
+ boolean into S.{' '}
+
It resulted in an error, which is what we want!
++ As you just saw, you can specify what’s allowed + for the type parameter(s) of a generic function. +
+ > + ) }, underConstructionCard ]}