TypeScript
1function add(a: number, b: number): number; 2function add(a: string, b: string): string; 3function add(a: any, b: any): any { 4 if (typeof a === "strng" && typeof b === "string") { 5 return a + " " + b; 6 } 7 return a + b; 8} 9 10console.log("Hello,", "World!"); 11console.log(1, 9); 12
変換後
JavaScript
1function add(a, b) { 2 if (typeof a === "strng" && typeof b === "string") { 3 return a + " " + b; 4 } 5 return a + b; 6} 7console.log("Hello,", "World!"); 8console.log(1, 9); 9
なぜ、TypeScriptでは function add(a: ???, b: ???): ???;
の部分を二回、別の型で宣言しているのですか?
TypeScriptの方で最初の二つの関数add()
をコメントアウトしてもエラーは出ませんでしたし、変換の結果も同じでした。
二回宣言する理由はなんですか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/04/12 06:55