読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第3節 開発環境を用意する

3.2 型チェックを実行する

src/calc.ts を作り、次の内容を書きます。

const calcSubtotal = (price: number, quantity: number): number => {
  return price * quantity;
};

const subtotal = calcSubtotal(220, "2");
console.log(subtotal);

ターミナルで型チェックを実行します。

npx tsc --noEmit
src/calc.ts:5:36 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
部分意味
src/calc.ts:5:36ファイル名、行、列
TS2345エラーの番号
Argument of type 'string'渡した引数の型は文字列
is not assignable to parameter of type 'number'数の引数には渡せない

--noEmit は、「型チェックだけをして、JavaScript のファイルは作らない」という指定です。"2" を 2 に直して、もう一度実行すると、何も表示されずに終わります。何も表示されないのは、エラーがないという意味です。

12 / 18