diff --git a/.prettierrc b/.prettierrc index 494de5f..351537b 100644 --- a/.prettierrc +++ b/.prettierrc @@ -5,7 +5,7 @@ { "files": "snippets/snippets/**/*.ts", "options": { - "printWidth": 45 + "printWidth": 49 } } ] diff --git a/README.md b/README.md index 8dbf7a3..3366e38 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# [TypeScript for Beginner Programmers](https://ts.chibicode.com/) +# TypeScript for Beginner Programmers ## License & Credits diff --git a/snippets/snippets/generics/cupt.ts b/snippets/snippets/generics/cupt.ts index 6773316..29911e8 100644 --- a/snippets/snippets/generics/cupt.ts +++ b/snippets/snippets/generics/cupt.ts @@ -9,8 +9,5 @@ function createState() { state = x } - return { - getState, - setState - } + return { getState, setState } } diff --git a/snippets/snippets/generics/gkgi.ts b/snippets/snippets/generics/gkgi.ts new file mode 100644 index 0000000..35a8142 --- /dev/null +++ b/snippets/snippets/generics/gkgi.ts @@ -0,0 +1,15 @@ +function createState() { + // Change to string + let state: string + + function getState() { + return state + } + + // Accepts a string + function setState(x: string) { + state = x + } + + return { getState, setState } +} diff --git a/snippets/snippets/generics/nnyl.ts b/snippets/snippets/generics/nnyl.ts new file mode 100644 index 0000000..d5ecab4 --- /dev/null +++ b/snippets/snippets/generics/nnyl.ts @@ -0,0 +1,14 @@ +function createState() { + let state: number + + function getState() { + return state + } + + // setState() expects a number + function setState(x: number) { + state = x + } + + return { getState, setState } +} diff --git a/snippets/snippets/generics/osaa.ts b/snippets/snippets/generics/osaa.ts new file mode 100644 index 0000000..0a64aad --- /dev/null +++ b/snippets/snippets/generics/osaa.ts @@ -0,0 +1,20 @@ +function createState() { + // Change to string + let state: string + + function getState() { + return state + } + + // Accepts a string + function setState(x: string) { + state = x + } + + return { getState, setState } +} + +const { getState, setState } = createState() + +setState('foo') +console.log(getState()) diff --git a/snippets/snippets/generics/stkh.ts b/snippets/snippets/generics/stkh.ts new file mode 100644 index 0000000..1ee3c09 --- /dev/null +++ b/snippets/snippets/generics/stkh.ts @@ -0,0 +1,5 @@ +const { getState, setState } = createState() + +// What happens if we use a string instead? +setState('foo') +console.log(getState()) diff --git a/snippets/snippets/generics/udpv.ts b/snippets/snippets/generics/udpv.ts index e5e5dc7..5dfdf0c 100644 --- a/snippets/snippets/generics/udpv.ts +++ b/snippets/snippets/generics/udpv.ts @@ -9,10 +9,7 @@ function createState() { state = x } - return { - getState, - setState - } + return { getState, setState } } const { getState, setState } = createState() diff --git a/snippets/snippets/generics/xeax.ts b/snippets/snippets/generics/xeax.ts new file mode 100644 index 0000000..5632156 --- /dev/null +++ b/snippets/snippets/generics/xeax.ts @@ -0,0 +1,4 @@ +const { getState, setState } = createState() + +setState('foo') +console.log(getState()) diff --git a/snippets/snippets/generics/zhql.ts b/snippets/snippets/generics/zhql.ts new file mode 100644 index 0000000..6a6a2a2 --- /dev/null +++ b/snippets/snippets/generics/zhql.ts @@ -0,0 +1,18 @@ +function createState() { + let state: number + + function getState() { + return state + } + + function setState(x: number) { + state = x + } + + return { getState, setState } +} + +const { getState, setState } = createState() + +setState('foo') +console.log(getState()) diff --git a/src/components/CodeBlock.tsx b/src/components/CodeBlock.tsx index 96ac6c0..b9efad4 100644 --- a/src/components/CodeBlock.tsx +++ b/src/components/CodeBlock.tsx @@ -4,7 +4,7 @@ import { useState } from 'react' import useTheme from 'src/hooks/useTheme' import Caption from 'src/components/Caption' import ButtonWithTouchActiveStates from 'src/components/ButtonWithTouchActiveStates' -import Emoji from 'src/components/Emoji' +import RunButtonText from 'src/components/RunButtonText' import PrismHighlight, { defaultProps } from 'prism-react-renderer' import theme from 'src/lib/prismTheme' @@ -15,7 +15,10 @@ const CodeBlock = ({ pointToRunButton, defaultResultVisible, caption, - noHighlight + noHighlight, + compile, + shouldHighlightResult, + resultError }: { snippet: string shouldHighlight?: (lineNumber: number, tokenNumber: number) => boolean @@ -24,6 +27,9 @@ const CodeBlock = ({ defaultResultVisible?: boolean caption?: React.ReactNode noHighlight?: boolean + compile?: boolean + shouldHighlightResult?: (lineNumber: number, tokenNumber: number) => boolean + resultError?: boolean }) => { const [resultVisible, setResultVisible] = useState(defaultResultVisible) const { radii, colors, ns, maxWidths, spaces, fontSizes } = useTheme() @@ -55,7 +61,7 @@ const CodeBlock = ({
@@ -134,14 +161,20 @@ const CodeBlock = ({
             css={css`
               max-width: ${maxWidths('sm')};
               margin-bottom: ${spaces(1.75)};
-              margin-left: ${spaces(0)};
-              margin-right: ${spaces(0)};
+              margin-left: ${spaces('-0.5')};
+              margin-right: ${spaces('-0.5')};
+
+              ${ns} {
+                margin-left: ${spaces(0)};
+                margin-right: ${spaces(0)};
+              }
             `}
           >
             {resultVisible ? (
               
- {result} + + {result} +
) : (
- Run + {pointToRunButton && ( <> diff --git a/src/components/RunButtonText.tsx b/src/components/RunButtonText.tsx index a1544d8..c575262 100644 --- a/src/components/RunButtonText.tsx +++ b/src/components/RunButtonText.tsx @@ -2,13 +2,13 @@ import { css, jsx } from '@emotion/core' import Emoji from 'src/components/Emoji' -const RunButtonText = () => ( +const RunButtonText = ({ compile }: { compile?: boolean }) => ( - Run + {compile ? 'Compile' : 'Run'} ) diff --git a/src/lib/snippets.ts b/src/lib/snippets.ts index 1e19e1a..c44392c 100644 --- a/src/lib/snippets.ts +++ b/src/lib/snippets.ts @@ -17,10 +17,23 @@ export const cupt = `function createState() { state = x } - return { - getState, - setState + return { getState, setState } +}` + +export const gkgi = `function createState() { + // Change to string + let state: string + + function getState() { + return state + } + + // Accepts a string + function setState(x: string) { + state = x } + + return { getState, setState } }` export const kiyi = `// Confused by generics code like this? @@ -29,21 +42,60 @@ function getProperty( key: K )` -export const udpv = `function createState() { +export const nnyl = `function createState() { let state: number function getState() { return state } + // setState() expects a number function setState(x: number) { state = x } - return { - getState, - setState + return { getState, setState } +}` + +export const osaa = `function createState() { + // Change to string + let state: string + + function getState() { + return state + } + + // Accepts a string + function setState(x: string) { + state = x + } + + return { getState, setState } +} + +const { getState, setState } = createState() + +setState('foo') +console.log(getState())` + +export const stkh = `const { getState, setState } = createState() + +// What happens if we use a string instead? +setState('foo') +console.log(getState())` + +export const udpv = `function createState() { + let state: number + + function getState() { + return state + } + + function setState(x: number) { + state = x } + + return { getState, setState } } const { getState, setState } = createState() @@ -53,3 +105,27 @@ console.log(getState()) setState(2) console.log(getState())` + +export const xeax = `const { getState, setState } = createState() + +setState('foo') +console.log(getState())` + +export const zhql = `function createState() { + let state: number + + function getState() { + return state + } + + function setState(x: number) { + state = x + } + + return { getState, setState } +} + +const { getState, setState } = createState() + +setState('foo') +console.log(getState())` diff --git a/src/lib/theme/colors.ts b/src/lib/theme/colors.ts index 0e03b75..9dd545a 100644 --- a/src/lib/theme/colors.ts +++ b/src/lib/theme/colors.ts @@ -7,11 +7,13 @@ export const allColors = { lightGreen: '#DFE9CE', brown: '#806538', lightBrown: '#E9D1AC', + darkOrange: '#EC9602', pink: '#FFD9CC', white: '#FFFFFF', white75: 'rgba(255, 255, 255, 0.75)', paleGreen: '#C8DCC7', - red: '#DB4003' + red: '#DB4003', + yellowHighlight: '#FFFF00' } const colors = (x: keyof typeof allColors) => allColors[x] diff --git a/src/pages/generics.tsx b/src/pages/generics.tsx index f630405..812c3a4 100644 --- a/src/pages/generics.tsx +++ b/src/pages/generics.tsx @@ -41,17 +41,22 @@ const Page = () => ( } />

- Similarly, if you feel that TypeScript generics are too - difficult—maybe you’ve only done frontend engineering in JS and - React/Vue, or you’re simply new to programming—this tutorial is - for you! I’ll try to help you actually understand generics. -

-

- (If you didn’t find TypeScript generics to be very difficult, this - tutorial might be too easy for you.) + Similarly, if you feel that TypeScript generics are too difficult, + this tutorial is for you! I’ll try to help you actually understand + generics.

- ) + ), + footer: { + content: ( + <> +

+ Note: If you didn’t find generics to be + difficult, this tutorial might be too easy for you. +

+ + ) + } }, { title: ( @@ -110,6 +115,67 @@ const Page = () => ( ) } }, + { + title: <>What if we use a string?, + content: ( + <> +

+ Now, instead of numbers like 1 or 2,{' '} + what happens if we use a string like{' '} + 'foo'? Try to guess first, and then{' '} + + press the button + + . +

+ + lineNumber === 3 && tokenNumber > 2 && tokenNumber < 4 + } + shouldHighlightResult={(lineNumber, tokenNumber) => + lineNumber === 3 && tokenNumber > 2 && tokenNumber < 4 + } + snippet={snippets.stkh} + result={ + <> + Argument of type '"foo"' is not assignable to parameter of + type 'number'. + + } + /> +

+ It failed to compile because setState() expects a + number as its argument. +

+ + lineNumber === 8 && tokenNumber > 4 && tokenNumber < 8 + } + /> +

+ To fix this, we can change the types from number to{' '} + string: +

+ + (lineNumber === 9 && tokenNumber > 4 && tokenNumber < 8) || + (lineNumber === 2 && tokenNumber > 4 && tokenNumber < 6) + } + /> +

+ It’ll now work!{' '} + + Press . + +

+ foo} /> + + ) + }, underConstructionCard ]} />