Skip to content

Commit 204ef81

Browse files
author
Orta
authored
Merge pull request #247 from takefumi-yoshii/translate/ja-playground-exs-primitives-literals
Translate primitives Literals of playground examples into japanese
2 parents f24812d + 71b433a commit 204ef81

File tree

1 file changed

+65
-0
lines changed
  • packages/playground-examples/copy/ja/TypeScript/Primitives

1 file changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// TypeScriptには、ソースコードにリテラルを用いた
2+
// とても面白い利用例があります。
3+
4+
// これは、型の拡張・型の絞り込みにおいて、多くのサポートをもたらします。
5+
// ( example:type-widening-narrowing )
6+
// そして、はじめにそれを網羅する価値があります。
7+
8+
// リテラルは集合型よりも具体的なサブタイプです。
9+
// どういうことかと言うと、型システム内部では
10+
// 「Hello World」は文字列ですが、文字列は「Hello World」ではありません。
11+
12+
const helloWorld = "Hello World";
13+
let hiWorld = "Hi World"; // これはletなので文字列型です
14+
15+
// この関数は、すべての文字列を受け入れます
16+
declare function allowsAnyString(arg: string);
17+
allowsAnyString(helloWorld);
18+
allowsAnyString(hiWorld);
19+
20+
// この関数は、文字列リテラル「Hello World」のみを受け入れます
21+
declare function allowsOnlyHello(arg: "Hello World");
22+
allowsOnlyHello(helloWorld);
23+
allowsOnlyHello(hiWorld);
24+
25+
// これにより、共用体型を使用して特定のリテラルのみを受け入れる
26+
// APIを宣言することができます
27+
28+
declare function allowsFirstFiveNumbers(arg: 1 | 2 | 3 | 4 | 5);
29+
allowsFirstFiveNumbers(1);
30+
allowsFirstFiveNumbers(10);
31+
32+
let potentiallyAnyNumber = 3;
33+
allowsFirstFiveNumbers(potentiallyAnyNumber);
34+
35+
// しかし、このルールは混み入ったオブジェクトには適用されません。
36+
37+
const myUser = {
38+
name: "Sabrina"
39+
};
40+
41+
// 定数として定義された `name:"Sabrina"` であっても
42+
// `name:string` に変換されてしまいます。
43+
// こうなるのは、nameプロパティがいつでも変更できるからです。
44+
45+
myUser.name = "Cynthia";
46+
47+
// なぜならmyUserのnameプロパティは変更できるため、
48+
// TypeScriptは型システムにおいてリテラル型を使用できません。
49+
// しかしながら、次の機能でこれを許容することができます。
50+
51+
const myUnchangingUser = {
52+
name: "Fatma"
53+
} as const;
54+
55+
// 「as const」をオブジェクトに適用すると、
56+
// 変更できるオブジェクトの代わりに、
57+
// 変更できないオブジェクトになります。
58+
59+
myUnchangingUser.name = "Raîssa";
60+
61+
// 「as const」はコード中でインラインリテラルを扱ったり、
62+
// 固定データを扱うための素晴らしいツールです。
63+
// 「as const」は配列でも動作します。
64+
65+
const exampleUsers = [{ name: "Brian" }, { name: "Fahrooq" }] as const;

0 commit comments

Comments
 (0)