Skip to content

Commit 86b8c94

Browse files
author
Orta
authored
Merge pull request #244 from takefumi-yoshii/translate/ja-playground-exs-primitives-any
Translate primitives any of playground examples into japanese
2 parents 4a04bb0 + 21e63fe commit 86b8c94

File tree

1 file changed

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

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// anyはTypeScriptのエスケープ句です。
2+
// any型を利用することで、一区切りのコードを
3+
// JavaScriptの様に動的に扱ったり、
4+
// 型システムの制限を回避することができます。
5+
6+
// any型が使用されている良い例はJSON.parseの結果です
7+
8+
const myObject = JSON.parse("{}");
9+
10+
// TypeScriptにおけるany宣言は、あなたがその値について詳しく知っていて
11+
// それが厳密に正しくないとしても、安全なものなので信じてくださいという宣言です。
12+
// 例えば、次のコードはクラッシュします。
13+
14+
myObject.x.y.z;
15+
16+
// any型を利用することで、型の安全性と引き換えに、
17+
// よりオリジナルに近いJavaScriptコードを書くことができます。
18+
19+
// any型は、(neverを除いて)どんな型でも割り当て可能であり、
20+
// 一方の型をもう一方に割り当て可能にする「型のワイルドカード」
21+
// によく似ています。
22+
23+
declare function debug(value: any);
24+
25+
debug("a string");
26+
debug(23);
27+
debug({ color: "blue" });
28+
29+
// いずれのdebug関数実行も、引数の型を
30+
// anyに置き換えることができるため許可されます。
31+
32+
// TypeScriptはany型の位置を考慮します。
33+
// たとえば、この様なタプル型を利用した関数引数であってもです。
34+
35+
declare function swap(x: [number, string]): [string, number];
36+
37+
declare const pair: [any, any];
38+
swap(pair);
39+
40+
// swap関数引数であるnumber型・string型のペアは、
41+
// any型のペアと置き換えることができるため、
42+
// この関数実行は許可されます。
43+
44+
// タプル型が初見の場合、example:tuples を参照してください。
45+
46+
// unknown型はany型の兄弟とも言うことができますが、
47+
// any型は「最善策を知っている」場合に利用する一方で、
48+
// unknown型は「最善策が分からないので、TSに型を伝える必要がある」
49+
// 場合に利用します。
50+
// 詳細は example:unknown-and-never を参照してください。

0 commit comments

Comments
 (0)