進捗:
01b/8

データ型とは
プログラムのデータを分類しよう

文字列、数値、真偽値など、データの種類を理解しよう

データ型って何?

データ型とは、 変数に入れることができるデータの種類 のことです。現実世界でも、箱に入れるものによって適切な箱の種類が違うのと同じように、プログラムでも扱うデータの種類によって適切な型があります。

🏠 現実世界の例

  • 数字 → 電卓に入力する
  • 文字 → 手紙に書く
  • Yes/No → スイッチのON/OFF
  • リスト → 買い物メモ

💻 プログラムの例

  • number → 数値(100, 3.14)
  • string → 文字列("太郎", "Hello")
  • boolean → 真偽値(true, false)
  • array → 配列([1, 2, 3])

TypeScriptの主要なデータ型

1 string型(文字列)

string は文字列を扱う型です。文字や文章、プレイヤー名、メッセージなど、テキスト情報を保存するときに使います。

基本的な書き方:

// ダブルクォート const playerName: string = "太郎"; const gameTitle: string = "My Game"; // シングルクォート(どちらでもOK) const message: string = 'ゲームクリア!'; const difficulty: string = 'Normal'; // バッククォート(テンプレートリテラル) const welcomeMessage: string = `こんにちは、${playerName}さん!`;

ゲームでの使用例

const playerName: string = "太郎"; const currentWeapon: string = "剣"; const statusMessage: string = "レベルアップ!"; const dialogText: string = "勇者よ、頑張れ!";

特徴

  • • クォートで囲む
  • • 文字数制限なし
  • • 日本語も使用可能
  • • 計算には使えない

2 number型(数値)

number は数値を扱う型です。整数、小数点、負の数など、すべての数値がnumber型です。計算やゲームの座標、ステータス値などに使用します。

基本的な書き方:

// 整数 const playerLevel: number = 5; const maxHealth: number = 100; // 小数点 const playerSpeed: number = 2.5; const gravity: number = 0.98; // 負の数 const temperatureChange: number = -10; const debt: number = -500; // 計算結果 const totalScore: number = 1500 + 250; // 1750 const halfHealth: number = maxHealth / 2; // 50

ゲームでの使用例

const playerX: number = 400; const playerY: number = 300; const health: number = 85; const score: number = 1250; const speed: number = 3.5; const damage: number = 25;

特徴

  • • クォート不要
  • • 四則演算可能
  • • 整数・小数区別なし
  • • 負の数も扱える

3 boolean型(真偽値)

booleantrue(真) または false(偽) の2つの値しか持たない型です。スイッチのON/OFF、ゲームの状態判定、条件の結果などに使用します。

基本的な書き方:

// 基本的な使い方 const isGameRunning: boolean = true; const isPaused: boolean = false; const hasKey: boolean = true; const isInvincible: boolean = false; // 条件式の結果 const isHealthLow: boolean = playerHealth < 20; const canLevelUp: boolean = playerExp >= 1000; const isGameOver: boolean = playerHealth <= 0;

ゲームでの使用例

const isJumping: boolean = false; const hasWeapon: boolean = true; const canAttack: boolean = true; const isInMenu: boolean = false; const gameCleared: boolean = false;

特徴

  • • trueまたはfalseのみ
  • • 条件判定に最適
  • • フラグ管理に使用
  • • ifステートメントで活用

🏷️ 型注釈 vs 型推論

TypeScriptでは、型を明示的に指定する方法と、自動で判別させる方法があります。

型注釈(明示的指定)

// 型を明示的に指定 const playerName: string = "太郎"; const playerLevel: number = 5; const isGameRunning: boolean = true;

メリット: 明確で読みやすい
推奨: 関数の引数や返り値

型推論(自動判別)

// TypeScriptが自動で型を判別 const playerName = "太郎"; // string型 const playerLevel = 5; // number型 const isGameRunning = true; // boolean型

メリット: シンプルで短い
推奨: 明らかな場合

🎮 実践例:RPGキャラクターの設定

キャラクターの基本情報を設定

// ========== キャラクター基本情報 ========== // 文字列型(string) const characterName: string = "勇者アレックス"; const characterClass: string = "戦士"; const currentWeapon: string = "鋼の剣"; const hometown: string = "平和村"; // 数値型(number) const level: number = 12; const experience: number = 2450; const health: number = 180; const maxHealth: number = 200; const mana: number = 50; const attack: number = 45; const defense: number = 32; const speed: number = 28; const gold: number = 1250; // 真偽値型(boolean) const isAlive: boolean = true; const hasKey: boolean = false; const canUseMagic: boolean = true; const isInBattle: boolean = false; const hasCompletedTutorial: boolean = true; const isVip: boolean = false; // ========== 計算による値の設定 ========== const healthPercentage: number = (health / maxHealth) * 100; // 90 const isHealthLow: boolean = healthPercentage < 25; // false const canLevelUp: boolean = experience >= 2500; // false const isFullHealth: boolean = health === maxHealth; // false // ========== 状況に応じた文字列生成 ========== const statusMessage: string = `${characterName}(Lv.${level})`; const healthDisplay: string = `HP: ${health}/${maxHealth}`; const battleStatus: string = isInBattle ? "戦闘中" : "平常時";

string型の活用

  • • キャラクター名
  • • 職業・武器名
  • • メッセージ表示
  • • UI表示文字

number型の活用

  • • ステータス値
  • • 経験値・レベル
  • • 所持金・アイテム数
  • • 計算結果

boolean型の活用

  • • 状態フラグ
  • • 条件判定結果
  • • アイテム所持状況
  • • ゲーム進行状態

⚠️ よくある間違いと対処法

❌ 型の不一致

let playerName: string = "太郎"; playerName = 123; // エラー!number型はstring型に代入できません

解決法: 同じ型の値を代入するか、適切な型変換を行いましょう

❌ 文字列の数値を計算に使用

const score1: string = "100"; const score2: string = "200"; const total = score1 + score2; // "100200" (文字列結合)

解決法: 数値として扱いたい場合はnumber型を使いましょう

❌ booleanとして扱いたい値の間違い

const isWinner: boolean = "true"; // エラー!文字列はboolean型ではありません const isActive: boolean = 1; // エラー!数値はboolean型ではありません

解決法: trueまたはfalseのキーワードを使いましょう

データ型のまとめ

string(文字列)

  • • クォートで囲む
  • • 文字・文章・名前
  • • UI表示テキスト

number(数値)

  • • 計算に使用可能
  • • 座標・ステータス
  • • 整数・小数対応

boolean(真偽値)

  • • true/falseのみ
  • • 条件判定
  • • 状態フラグ

データ型は プログラムの設計図 です。適切な型を選ぶことで、エラーを防ぎ、読みやすいコードが書けるようになります。

次章では、「何もない」を表す null の概念について学習します!