Skip to content

Commit 3233229

Browse files
author
Orta
authored
Merge pull request #537 from sasurau4/add/playground-example-to-ja
Add/playground example to ja
2 parents 53e6f12 + f181cf9 commit 3233229

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//// { compiler: { }, order: 3 }
2+
3+
// TypeScriptのエラーメッセージはときどき、少しだけ冗長になってしまうことがあります。
4+
// 3.7では、特にひどいいくつかの事例を
5+
// 取り上げました。
6+
7+
// 入れ子のプロパティ
8+
9+
let a = { b: { c: { d: { e: "string" } } } };
10+
let b = { b: { c: { d: { e: 12 } } } };
11+
12+
a = b;
13+
14+
// 以前は、入れ子のプロパティ1つにつき2行のコードでした。
15+
// これは、エラーメッセージの最初と最後の行を読むことで、
16+
// エラーメッセージの読み方をすぐに学べるということを意味していました。
17+
18+
// 今は、これらはインラインになりました。:tada:
19+
20+
// 3.6では以下のようになっていました:
21+
//
22+
// Type '{ b: { c: { d: { e: number; }; }; }; }' is not assignable to type '{ b: { c: { d: { e: string; }; }; }; }'.
23+
// Types of property 'b' are incompatible.
24+
// Type '{ c: { d: { e: number; }; }; }' is not assignable to type '{ c: { d: { e: string; }; }; }'.
25+
// Types of property 'c' are incompatible.
26+
// Type '{ d: { e: number; }; }' is not assignable to type '{ d: { e: string; }; }'.
27+
// Types of property 'd' are incompatible.
28+
// Type '{ e: number; }' is not assignable to type '{ e: string; }'.
29+
// Types of property 'e' are incompatible.
30+
// Type 'number' is not assignable to type 'string'
31+
32+
// これによって、様々な形のオブジェクトに対しても、
33+
// 有用で簡潔なエラーメッセージを表示することができるようになりました。
34+
35+
class ExampleClass {
36+
state = "ok";
37+
}
38+
39+
class OtherClass {
40+
state = 12;
41+
}
42+
43+
let x = { a: { b: { c: { d: { e: { f: ExampleClass } } } } } };
44+
let y = { a: { b: { c: { d: { e: { f: OtherClass } } } } } };
45+
x = y;
46+
47+
// 3.6では以下のようになっていました:
48+
//
49+
// Type '{ a: { b: { c: { d: { e: { f: typeof OtherClass; }; }; }; }; }; }' is not assignable to type '{ a: { b: { c: { d: { e: { f: typeof ExampleClass; }; }; }; }; }; }'.
50+
// Types of property 'a' are incompatible.
51+
// Type '{ b: { c: { d: { e: { f: typeof OtherClass; }; }; }; }; }' is not assignable to type '{ b: { c: { d: { e: { f: typeof ExampleClass; }; }; }; }; }'.
52+
// Types of property 'b' are incompatible.
53+
// Type '{ c: { d: { e: { f: typeof OtherClass; }; }; }; }' is not assignable to type '{ c: { d: { e: { f: typeof ExampleClass; }; }; }; }'.
54+
// Types of property 'c' are incompatible.
55+
// Type '{ d: { e: { f: typeof OtherClass; }; }; }' is not assignable to type '{ d: { e: { f: typeof ExampleClass; }; }; }'.
56+
// Types of property 'd' are incompatible.
57+
// Type '{ e: { f: typeof OtherClass; }; }' is not assignable to type '{ e: { f: typeof ExampleClass; }; }'.
58+
// Types of property 'e' are incompatible.
59+
// Type '{ f: typeof OtherClass; }' is not assignable to type '{ f: typeof ExampleClass; }'.
60+
// Types of property 'f' are incompatible.
61+
// Type 'typeof OtherClass' is not assignable to type 'typeof ExampleClass'.
62+
// Type 'OtherClass' is not assignable to type 'ExampleClass'.
63+
// Types of property 'state' are incompatible.
64+
// Type 'number' is not assignable to type 'string'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// { compiler: { }, order: 2 }
2+
3+
// Null合体演算子は左辺が
4+
// nullまたはundefinedのときに
5+
// 右辺の式を返す||演算子の代わりです。
6+
7+
// Null合体演算子とは対照的に、||演算子は空文字列や数字の0がfalseと判断される
8+
// falsyチェックを用います。
9+
10+
// この機能の良い例は、
11+
// キーが渡されなかったときに部分オブジェクトがデフォルト値を持つように処理する操作です。
12+
13+
interface AppConfiguration {
14+
// デフォルト: "(no name)"; 空文字列は有効
15+
name: string;
16+
17+
// デフォルト: -1; 0は有効
18+
items: number;
19+
20+
// デフォルト: true
21+
active: boolean;
22+
}
23+
24+
function updateApp(config: Partial<AppConfiguration>) {
25+
// Null合体演算子を使ったとき
26+
config.name = config.name ?? "(no name)";
27+
config.items = config.items ?? -1;
28+
config.active = config.active ?? true;
29+
30+
// Null合体演算子を使わない現在の解決法
31+
config.name = typeof config.name === "string" ? config.name : "(no name)";
32+
config.items = typeof config.items === "number" ? config.items : -1;
33+
config.active = typeof config.active === "boolean" ? config.active : true;
34+
35+
// ||演算子を用いると有効でないデータになる可能性がある
36+
config.name = config.name || "(no name)"; // "" の入力が許容できない
37+
config.items = config.items || -1; // 0の入力が許容できない
38+
config.active = config.active || true; // とても悪いことに、常にtrueになる
39+
}
40+
41+
// 3.7のリリース記事にて、Null合体演算子についてより詳細に知ることができます:
42+
//
43+
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//// { compiler: { }, order: 1 }
2+
3+
// オプショナルチェイニングは3.7の開発中にTC39のStage 3合意に達しました。
4+
// オプショナルチェイニングによって
5+
// nullまたはundefinedだったときに
6+
// 式の実行を即座に止めるコードが書けるようになります。
7+
8+
// プロパティへのアクセス
9+
10+
// データの中にアーティストそのものの情報やアーティストのプロフィールがない可能性がある
11+
// アルバム情報を想像してみましょう。例えば、コンピレーションアルバムは
12+
// 単一のアーティストについての情報を持っていないでしょう。
13+
14+
type AlbumAPIResponse = {
15+
title: string;
16+
artist?: {
17+
name: string;
18+
bio?: string;
19+
previousAlbums?: string[];
20+
};
21+
};
22+
23+
declare const album: AlbumAPIResponse;
24+
25+
// オプショナルチェイニングを用いると、
26+
// コードは以下のように書けます。
27+
28+
const artistBio = album?.artist?.bio;
29+
30+
// 以下のように書く代わりに:
31+
32+
const maybeArtistBio = album.artist && album.artist.bio;
33+
34+
// 演算子は"falsy"な値(例えば、空文字列や0、NaN、もちろんfalse)に
35+
// 対して異なる振る舞いをするため、
36+
// この場合、?.演算子は&&演算子とは異なる振る舞いをします。
37+
38+
// オプショナルチェイニングはnullまたはundefinedのみを
39+
// 処理を止め、undefinedを返す合図と捉えます。
40+
41+
// オプショナルな要素へのアクセス
42+
43+
// プロパティへのアクセスは .演算子を用いて行われます。
44+
// オプショナルチェイニングは要素にアクセス際の[]演算子でも同様に機能します。
45+
46+
const maybeArtistBioElement = album?.["artist"]?.["bio"];
47+
48+
const maybeFirstPreviousAlbum = album?.artist?.previousAlbums?.[0];
49+
50+
// オプショナルな呼び出し
51+
52+
// オプショナルチェイニングは、実行時に存在するか分からない関数を扱うときに、
53+
// 関数が存在するときにだけ呼び出す機能をサポートしています。
54+
// これによって、伝統的に書いていた次のような
55+
// コードを置き換えられます: if (func) func()
56+
57+
// 以下はAPI requestからのcallbackに対する
58+
// オプショナルな呼び出しの例です。
59+
60+
const callUpdateMetadata = (metadata: any) => Promise.resolve(metadata); // API呼び出しのダミー
61+
62+
const updateAlbumMetadata = async (metadata: any, callback?: () => void) => {
63+
await callUpdateMetadata(metadata);
64+
65+
callback?.();
66+
};
67+
68+
// 3.7のリリース記事にて、オプショナルチェイニングについてより詳細に知ることができます:
69+
//
70+
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/

0 commit comments

Comments
 (0)