1-1. 基本的な型を使う

string, number, booleanの基本的な型を学びます

JS/TS基礎 - 第1章: TypeScript基礎 - 型の基本

課題:基本的な型を使ってみよう

TypeScriptでは、変数にを指定することができます。 型を指定すると、間違った値を入れようとしたときにエラーで教えてくれます。

やること

  1. 1.文字列型の変数 message を作る(値: "Hello, TypeScript!"
  2. 2.数値型の変数 age を作る(値: 25
  3. 3.真偽値型の変数 isStudent を作る(値: true
  4. 4.各変数を print() で画面に表示する

型の書き方

TypeScriptでは以下のように型を指定します:

const 変数名: 型 = 値;

例:

const name: string = "太郎";
const score: number = 100;
const isActive: boolean = true;

※ 画面への表示:このレッスンでは print(変数名) で結果を画面に表示できます。

期待される出力

Hello, TypeScript!
25
true
// ヘルパー関数(変更不要)
function print(value: any): void {
  const output = document.getElementById('output');
  if (output) {
    const div = document.createElement('div');
    div.style.margin = '8px 0';
    div.style.fontFamily = 'monospace';
    div.textContent = String(value);
    output.appendChild(div);
  }
}

// ここからあなたのコードを書いてください!

// 1. 文字列型の変数 message


// 2. 数値型の変数 age


// 3. 真偽値型の変数 isStudent


// 4. print()で表示