Skip to content

Commit 723c7fc

Browse files
committed
chore: more notes on typescript typing course
1 parent cc2a661 commit 723c7fc

File tree

4 files changed

+906
-549
lines changed

4 files changed

+906
-549
lines changed

public/content/.obsidian/core-plugins.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@
1313
"markdown-importer",
1414
"outline",
1515
"word-count",
16-
"open-with-default-app",
1716
"file-recovery"
18-
]
17+
]

public/content/.obsidian/workspace

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"state": {
1010
"type": "markdown",
1111
"state": {
12-
"file": "javascript/testing-javascript/6-Testing-React-Components-With-Jest-And-React-Testing-Library.md",
12+
"file": "typescript/Noted-To-Merge.md",
1313
"mode": "source",
1414
"source": false
1515
}
@@ -69,7 +69,7 @@
6969
"state": {
7070
"type": "backlink",
7171
"state": {
72-
"file": "javascript/testing-javascript/6-Testing-React-Components-With-Jest-And-React-Testing-Library.md",
72+
"file": "typescript/Noted-To-Merge.md",
7373
"collapseAll": false,
7474
"extraContext": false,
7575
"sortOrder": "alphabetical",
@@ -86,7 +86,7 @@
8686
"state": {
8787
"type": "outgoing-link",
8888
"state": {
89-
"file": "javascript/testing-javascript/6-Testing-React-Components-With-Jest-And-React-Testing-Library.md",
89+
"file": "typescript/Noted-To-Merge.md",
9090
"linksCollapsed": false,
9191
"unlinkedCollapsed": true
9292
}
@@ -109,7 +109,7 @@
109109
"state": {
110110
"type": "outline",
111111
"state": {
112-
"file": "javascript/testing-javascript/6-Testing-React-Components-With-Jest-And-React-Testing-Library.md"
112+
"file": "typescript/Noted-To-Merge.md"
113113
}
114114
}
115115
}
@@ -122,6 +122,13 @@
122122
},
123123
"active": "81afcea829cb47b4",
124124
"lastOpenFiles": [
125+
"typescript/ts-fundamentals-v2.md",
126+
"typescript/production-typescript-course.md",
127+
"typescript/getting-started.md",
128+
"typescript/enforcing-prop-names.md",
129+
"typescript/developers-guide.md",
130+
"typescript/duck-typing.md",
131+
"typescript/Typescript.md",
125132
"javascript/testing-javascript/6-Testing-React-Components-With-Jest-And-React-Testing-Library.md",
126133
"javascript/testing-javascript/5-Configure-Jest-For-Testing-JS-Apps.md"
127134
]
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Making TypeScript Stick
2+
3+
## Compiling TypeScript
4+
5+
### Will it compile?
6+
7+
```ts
8+
// YES
9+
let age = 38
10+
11+
age = Number.NaN
12+
13+
// YES - Unfortunately, because tuples are a specialized flavor of arrays (and at runtime, they actually are just regular arrays) they expose the entire array API. Look at the type signature of `.push()`
14+
const vector3: [number, number, number] = [3, 4, 5]
15+
16+
vector3.push(6)
17+
18+
// NO - Duplicate identifier
19+
type Color = {
20+
red: number
21+
green: number
22+
blue: number
23+
}
24+
25+
interface Color {
26+
alpha: number
27+
}
28+
29+
// NO - no initializer and is not definitely assigned in constructor
30+
class Person {
31+
name: string
32+
friends: Person[]
33+
34+
constructor(name: string) {
35+
this.name = name
36+
}
37+
}
38+
39+
// NO - Property 'name' in type 'Student' is not assignable to the same property in base type 'Person'.
40+
abstract class Person {
41+
public abstract name: string
42+
}
43+
44+
class Student extends Person {
45+
public name: string | string[] = ['Mike North']
46+
}
47+
48+
// NO - not assignable to type Color
49+
interface Color {
50+
red: number
51+
green: number
52+
blue: number
53+
}
54+
55+
function printColor(color: Color) {
56+
// ... //
57+
}
58+
59+
printColor({
60+
red: 255,
61+
green: 0,
62+
blue: 0,
63+
alpha: 0.4,
64+
})
65+
```
66+
67+
## Challenges
68+
69+
### Typerdy
70+
71+
This section was a typing challenge.
72+
73+
## Type Challenges
74+
75+
This is a [repo](https://github.com/type-challenges/type-challenges) to practice your typing.
76+
77+
The difficulty is subjective, so don't get too frustrated if some of the medium come off as difficult.
78+
79+
Challenge 1:
80+
81+
```ts
82+
// @errors: 2344
83+
84+
type Expect<T extends true> = T
85+
86+
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
87+
? 1
88+
: 2
89+
? true
90+
: false
91+
92+
type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true
93+
94+
// ---cut---
95+
96+
// Implement this type
97+
98+
type If<C, T, F> = never
99+
100+
// Tests
101+
102+
type cases = [
103+
Expect<Equal<If<true, 'apple', 'pear'>, 'apple'>>,
104+
105+
Expect<Equal<If<false, 'orange', 42>, 42>>
106+
]
107+
```
108+
109+
The answer:
110+
111+
```ts
112+
// Implement this type
113+
114+
type If<C, T, F> = C extends true ? T : F
115+
```
116+
117+
Challenge 2:
118+
119+
```ts
120+
// @errors: 2344
121+
122+
type Expect<T extends true> = T
123+
124+
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
125+
? 1
126+
: 2
127+
? true
128+
: false
129+
130+
type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true
131+
132+
// ---cut---
133+
134+
// Implement this type
135+
136+
type LengthOfTuple<T> = never
137+
138+
// Tests
139+
140+
const Fruits = ['cherry', 'banana'] as const
141+
142+
type cases = [
143+
Expect<Equal<LengthOfTuple<[1, 2, 3]>, 3>>,
144+
Expect<NotEqual<LengthOfTuple<[1, 2, 3]>, 2>>,
145+
Expect<Equal<LengthOfTuple<typeof Fruits>, 2>>,
146+
Expect<Equal<LengthOfTuple<[]>, 0>>
147+
]
148+
```
149+
150+
The answer:
151+
152+
```ts
153+
// Implement this type
154+
155+
type LengthOfTuple<T> = T extends readonly any[] ? T['length'] : never
156+
```

0 commit comments

Comments
 (0)