【TypeScript】関数の定義(引数・戻り値あり)

TypeScriptで関数(引数・戻り値あり)を定義する方法について入門者向けにまとめました。

## 【関数】(引数・戻り値あり)

TypeScriptで関数(引数・戻り値あり)を定義するには、次のように記述します。

// 関数(引数xの型:number、戻り値の型:string)
function myFunc(x: number): string {
  x = x * 1000;
  return x.toLocaleString() + '点';
}



export class AppComponent {
  x = myFunc(2);
};

## 【関数】戻り値を配列にする場合

TypeScriptで戻り値を配列にする場合、次のように記述します。

// 関数(戻り値の配列型:string)
function myFunc(): string[] {
  return new Array("A","B","C")
}

export class AppComponent {
  x = myFunc();
};
参考文献・関連記事
参考 【TypeScript入門】使い方まとめ
参考 【AngularJS入門】基礎とサンプル集
関連 【Ionic入門】Android・iPhoneアプリ開発編
TypeScript
スポンサーリンク

コメント