Skip to content

Commit 54dff01

Browse files
committed
finish sentences 4-4 to 4-7
1 parent 16d93f6 commit 54dff01

10 files changed

+63
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type Flatten<T> = T extends Array<infer U> ? U : T;
2+
3+
const num = 5;
4+
const arr = [3, 6, 9];
5+
type A = Flatten<typeof arr>;
6+
type N = Flatten<typeof num>;
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type User = { id: unknown };
2+
type NewUser = User & { id: string };
3+
type OldUser = User & { id: number };
4+
type Book = { isbn: string };
5+
6+
type IdOf<T> = T extends User ? T['id'] : never;
7+
8+
type NewUserId = IdOf<NewUser>;
9+
type OldUserId = IdOf<OldUser>;
10+
type BookId = IdOf<Book>;

04-typescript/05-advanced/extends.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ const merge = <T, U extends T>(obj1: T, obj2: U): T & U => ({
44
});
55

66
override({ a: 1 }, { a: 24, b: 8 });
7-
// override({ a: 15 }, { x: 73 }); /* compile error */
7+
// override({ a: 2 }, { x: 73 }); /* compile error */

04-typescript/05-advanced/record.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Animal = 'cat' | 'dog' | 'rabbit';
2+
type AnimalNote = Record<Animal, string>;
3+
4+
const animalKanji: AnimalNote = {
5+
cat: '猫',
6+
dog: '犬',
7+
rabbit: '兎',
8+
};
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type Company = 'Apple' | 'IBM' | 'GitHub';
2+
3+
type C1 = Lowercase<Company>;
4+
type C2 = Uppercase<Company>;
5+
type C3 = Uncapitalize<Company>;
6+
type C4 = Capitalize<C3>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type DateFormat = `${number}-${number}-${number}`;
2+
3+
const date: DateFormat = "2020-12-05";
4+
// const date2: DateFormat = 'Dec. 5, 2020';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const q1 = 'SELECT * FROM users';
2+
const q2 = 'SELECT id, body, createdAt FROM posts';
3+
const q3 = 'SELECT userId, postId FROM comments';
4+
5+
type PickTable<T extends string> = T extends `SELECT ${string} FROM ${infer U}` ? U : never;
6+
7+
type Tables = PickTable<typeof q1 | typeof q2 | typeof q3>; // 'users' | 'posts' | 'comments'
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const tables = ['users', 'posts', 'comments'] as const;
2+
type Table = typeof tables[number];
3+
type AllSelect = `SELECT * FROM ${Table}`;
4+
type LimitSelect = `${AllSelect} LIMIT ${number}`;
5+
6+
const createQuery = (table: Table, limit?: number): AllSelect | LimitSelect =>
7+
limit ? `SELECT * FROM ${table} LIMIT ${limit}` as const
8+
: `SELECT * FROM ${table}` as const;
9+
10+
const query = createQuery('users', 20);
11+
console.log(query);

04-typescript/06-type-guard/user-defined.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const u2: unknown = JSON.parse('{ "username": "patty", "address": "Maple Town" }
1616
const u3: unknown = JSON.parse(
1717
'{ "username": "patty", "address": { "zipcode": "111", "town": "Maple Town" } }',
1818
);
19+
1920
[u1, u2, u3].forEach((u) => {
2021
if (isUser(u)) {
2122
console.log(`${u.username} lives in ${u.address.town}`);

CHANGELOG.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313

1414
### 第1章&nbsp; こんにちは React
1515

16-
- 従来 Mac のみで説明していた環境作成を Windows についても言及、WSL をベースにした手順を[オンラインドキュメント](../extract/build-win-wnv.md)として追加した
17-
- プロジェクトパッケージのロックファイルについて、Yarn に加え npm コマンド使用時の `package-lock.json` についても言及
16+
- 「1-1. 基本環境の構築」の「Node.js をインストールする」項にて、従来 Mac のみで説明していた環境作成を Windows についても言及、WSL をベースにした手順を[オンラインドキュメント](../extract/build-win-wnv.md)として追加した
17+
- 「1-3. アプリを管理するためのコマンドやスクリプト/Yarn コマンド」にて、プロジェクトパッケージのロックファイルについて、npm コマンド使用時の `package-lock.json` についても言及
18+
19+
### 第4章&nbsp; TypeScript で型を強める
20+
21+
- 「ユニオン型」を「共用体型」に、「インターセクション型」を「交差型」に呼び方を変更
22+
- 「4-5. さらに高度な型表現」の「型表現に使われる演算子」項にて、配列の要素から型を作成する書き方の説明を追加
23+
- 「4-5. さらに高度な型表現」に新しく「条件付き型とテンプレートリテラル型」項を追加
24+
- 「4-5. さらに高度な型表現」の「組み込みユーティリティ型」項にて、`Record` 型および文字列リテラルの各ユーティリティ型についての説明を追加
1825

1926
## 第3版(2020-08-20)
2027

0 commit comments

Comments
 (0)