Skip to content

Commit d96d87b

Browse files
committed
Add arrow functions and bigint/symbol exercises
- Add arrow functions exercise (04.functions/04.arrow-functions) teaching syntax, implicit returns, and when to use each form - Add bigint and symbol exercise step (01.primitive-types/04.bigint-and-symbol) - Update exercise READMEs and FINISHED files to reflect new content
1 parent adb77e5 commit d96d87b

File tree

18 files changed

+463
-18
lines changed

18 files changed

+463
-18
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# BigInt and Symbol
2+
3+
👨‍💼 TypeScript has two more primitive types that are less commonly used but good
4+
to know about: `bigint` and `symbol`.
5+
6+
## BigInt - Large Integers
7+
8+
Regular `number` has limits—it can't accurately represent integers larger than
9+
`Number.MAX_SAFE_INTEGER` (about 9 quadrillion). `bigint` handles arbitrarily
10+
large integers:
11+
12+
```ts
13+
const big: bigint = 9007199254740993n // Note the 'n' suffix
14+
const alsoBig: bigint = BigInt('9007199254740993')
15+
16+
// BigInt arithmetic
17+
const sum = 1000000000000000000n + 1n // Works correctly!
18+
```
19+
20+
<callout-warning>
21+
BigInt and number don't mix directly. You can't do `5n + 3`—you need to
22+
convert one type to the other.
23+
</callout-warning>
24+
25+
## Symbol - Unique Identifiers
26+
27+
Symbols are guaranteed-unique values, useful as object keys when you need to
28+
avoid name collisions:
29+
30+
```ts
31+
const id: symbol = Symbol('userId')
32+
const anotherId: symbol = Symbol('userId')
33+
34+
id === anotherId // false - each Symbol() creates a unique value!
35+
```
36+
37+
Symbols are often used for "hidden" object properties or library-internal keys.
38+
39+
🐨 Open <InlineFile file="index.ts" /> and:
40+
41+
1. Create a bigint literal with the `n` suffix
42+
2. Perform arithmetic with bigint values
43+
3. Create symbols and observe their uniqueness
44+
45+
💰 BigInt syntax:
46+
47+
```ts
48+
const big: bigint = 123456789012345678901234567890n
49+
```
50+
51+
📜 [MDN - BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
52+
📜 [MDN - Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// BigInt and Symbol
2+
// Less common but important primitive types
3+
4+
// 🐨 Create a bigint variable `largeNumber` with the value 9007199254740993n
5+
// This is larger than Number.MAX_SAFE_INTEGER!
6+
// 💰 const largeNumber: bigint = 9007199254740993n
7+
8+
// 🐨 Create another bigint `anotherLarge` with value 1000000000000000000n
9+
10+
// 🐨 Add them together into a variable called `sum`
11+
// 💰 const sum = largeNumber + anotherLarge
12+
13+
// 🐨 Create a symbol called `userId` with description 'user-id'
14+
// 💰 const userId: symbol = Symbol('user-id')
15+
16+
// 🐨 Create another symbol called `anotherId` with the same description 'user-id'
17+
18+
// 🐨 Create a variable `areEqual` that compares userId === anotherId
19+
// This will be false because each Symbol() call creates a unique value!
20+
21+
// Test - uncomment when ready
22+
// console.log('Large number:', largeNumber)
23+
// console.log('Sum of bigints:', sum)
24+
// console.log('userId:', userId)
25+
// console.log('anotherId:', anotherId)
26+
// console.log('Are symbols equal?', areEqual) // false
27+
28+
export {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# BigInt and Symbol
2+
3+
👨‍💼 Nice work! You now know all of TypeScript's primitive types.
4+
5+
🦉 When to use these types:
6+
7+
**BigInt:**
8+
9+
- Financial calculations requiring precision beyond `number`
10+
- Cryptographic operations
11+
- Working with database IDs that exceed safe integer limits
12+
- Any math requiring very large integers
13+
14+
**Symbol:**
15+
16+
- Creating unique object keys that won't collide with string keys
17+
- Library-internal properties that shouldn't be accessed directly
18+
- Well-known symbols like `Symbol.iterator` for custom iteration
19+
20+
In practice, `number`, `string`, `boolean`, `null`, and `undefined` cover 99% of
21+
use cases. But it's good to know `bigint` and `symbol` exist when you need them!
22+
23+
```ts
24+
// The complete set of TypeScript primitives:
25+
const str: string = 'hello'
26+
const num: number = 42
27+
const bool: boolean = true
28+
const nul: null = null
29+
const undef: undefined = undefined
30+
const big: bigint = 9007199254740993n
31+
const sym: symbol = Symbol('unique')
32+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { testStep, expect } from '@epic-web/workshop-utils/test'
2+
import {
3+
largeNumber,
4+
anotherLarge,
5+
sum,
6+
userId,
7+
anotherId,
8+
areEqual,
9+
} from './index.ts'
10+
11+
await testStep('largeNumber should be a bigint', async () => {
12+
expect(
13+
typeof largeNumber,
14+
'🚨 largeNumber should be type bigint - use the n suffix: 9007199254740993n',
15+
).toBe('bigint')
16+
expect(
17+
largeNumber,
18+
'🚨 largeNumber should be 9007199254740993n',
19+
).toBe(9007199254740993n)
20+
})
21+
22+
await testStep('anotherLarge should be a bigint', async () => {
23+
expect(
24+
typeof anotherLarge,
25+
'🚨 anotherLarge should be type bigint',
26+
).toBe('bigint')
27+
expect(
28+
anotherLarge,
29+
'🚨 anotherLarge should be 1000000000000000000n',
30+
).toBe(1000000000000000000n)
31+
})
32+
33+
await testStep('sum should be the addition of both bigints', async () => {
34+
expect(
35+
typeof sum,
36+
'🚨 sum should be type bigint',
37+
).toBe('bigint')
38+
expect(
39+
sum,
40+
'🚨 sum should be largeNumber + anotherLarge',
41+
).toBe(largeNumber + anotherLarge)
42+
})
43+
44+
await testStep('userId and anotherId should be symbols', async () => {
45+
expect(
46+
typeof userId,
47+
'🚨 userId should be type symbol - use Symbol("user-id")',
48+
).toBe('symbol')
49+
expect(
50+
typeof anotherId,
51+
'🚨 anotherId should be type symbol',
52+
).toBe('symbol')
53+
})
54+
55+
await testStep('symbols with same description should not be equal', async () => {
56+
expect(
57+
areEqual,
58+
'🚨 areEqual should be false - each Symbol() creates a unique value even with the same description',
59+
).toBe(false)
60+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// BigInt and Symbol
2+
// Less common but important primitive types
3+
4+
// BigInt for large integers
5+
const largeNumber: bigint = 9007199254740993n
6+
const anotherLarge: bigint = 1000000000000000000n
7+
const sum: bigint = largeNumber + anotherLarge
8+
9+
// Symbols are unique
10+
const userId: symbol = Symbol('user-id')
11+
const anotherId: symbol = Symbol('user-id')
12+
const areEqual: boolean = userId === anotherId // false!
13+
14+
console.log('Large number:', largeNumber)
15+
console.log('Another large:', anotherLarge)
16+
console.log('Sum of bigints:', sum)
17+
console.log('userId:', userId)
18+
console.log('anotherId:', anotherId)
19+
console.log('Are symbols equal?', areEqual) // false - each Symbol is unique
20+
21+
export { largeNumber, anotherLarge, sum, userId, anotherId, areEqual }

exercises/01.primitive-types/FINISHED.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ You learned:
99
- 📝 **Strings** hold text data and support template literals
1010
-**Booleans** are the foundation of conditional logic
1111
-**Null and undefined** represent absence of value in different ways
12+
- 🔢 **BigInt** handles integers larger than `Number.MAX_SAFE_INTEGER`
13+
- 🔑 **Symbol** creates unique identifiers that never collide
1214

1315
🦉 A key insight: TypeScript's type inference often figures out types for you.
1416
You don't always need to write `: number` or `: string`—TypeScript can infer

exercises/01.primitive-types/README.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ const name: string = 'Alice'
99
const isActive: boolean = true
1010
```
1111

12-
These three types cover most of what you'll work with day-to-day:
12+
The core types you'll use most often:
1313

1414
- **`number`** - All numeric values (integers, decimals, negatives)
1515
- **`string`** - Text data (always in quotes)
1616
- **`boolean`** - True or false values
17+
- **`null`** - Intentional absence of a value
18+
- **`undefined`** - Variable not yet assigned
19+
- **`bigint`** - Arbitrarily large integers
20+
- **`symbol`** - Unique identifiers
1721

1822
## Why Types Matter
1923

exercises/02.variables/02.solution.reassignment-vs-mutation/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ can be rearranged (mutation).
1313
🦉 If you want truly immutable data in TypeScript, you can use:
1414

1515
- `as const` for literal values
16-
- `readonly` arrays: `readonly string[]`
16+
- `readonly` arrays: `ReadonlyArray<string>`
1717
- Libraries like Immer for complex immutability
1818

1919
But for now, just remember: `const` = "I won't reassign this variable" not "this

exercises/02.variables/02.solution.reassignment-vs-mutation/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Understanding Reassignment vs Mutation
22

33
// This array is declared with const
4-
const shoppingList: string[] = ['milk', 'eggs', 'bread']
4+
const shoppingList: Array<string> = ['milk', 'eggs', 'bread']
55

66
console.log('Original list:', shoppingList)
77

exercises/03.control-flow/02.solution.loops/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Shopping Cart Calculator
22
// Using loops to process arrays
33

4-
const prices: number[] = [29.99, 9.99, 49.99, 4.99, 19.99]
4+
const prices: Array<number> = [29.99, 9.99, 49.99, 4.99, 19.99]
55

66
let total = 0
77

0 commit comments

Comments
 (0)