スポンサーリンク
今回もtypescriptを勉強していきます!
今回は、関数の型について
void
返り値のない関数は、返り値にvoidと書きます
function greet(name: string): void {
console.log("hello", name);
}
greet("yamada");
返り値がある場合
function getPrice(price: number): string {
return price + "円";
}
const price = getPrice(100);
console.log("price", price);
返り値の型の書き方をおさえておけば、
とりあえずは大丈夫な気がします。
アロー関数
ついでにモダンなアロー関数の書き方もおさえておきましょか
const getPrice = (price: number): string => {
return price.toLocaleString() + "円";
}
console.log(getPrice(2000))
このように書けますし、もっと短く書くなら
const getPrice = (price: number): string => price.toLocaleString() + "円"; console.log(getPrice(2000))
このようにも書けますね!
アロー関数の書き方がわからないという人もいると思いますが、
JSの新しい関数の書き方とおぼえておけばOKです。
ぜひ慣れてみてください〜^^
typescriptのコンパイル方法から学びたいかたはこちら。




