|
| 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