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}
+
- 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,{' '}
+ 'foo'? Try to guess first, and then{' '}
+
+ It failed to compile because setState() expects a
+ number as its argument.
+
+ To fix this, we can change the types from number to{' '}
+ string:
+
+ It’ll now work!{' '}
+