この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:6
ページ更新者:guest
更新日時:2026-06-11 07:12:00

タイトル: 型の確認
SEOタイトル: 各言語での型確認方法完全ガイド — Python/JS/Java/PHP

この記事の要点
  • Python: type(x) / isinstance(x, int)。継承を考慮するなら isinstance 推奨
  • JavaScript: typeof x (プリミティブ判定)、Array.isArray()instanceof、配列/null 判定の落とし穴に注意
  • Java: x.getClass()instanceof (Java 16+ では pattern matching x instanceof String s)
  • PHP: gettype() / is_int() / is_string() 系 / get_class() / instanceof
  • デバッガ / 型ヒント (Python typing、PHP 8 引数型、TypeScript) を併用すれば実行時確認の頻度は減らせる

型を確認する場面

動的型付け言語 (Python / JavaScript / PHP / Ruby) では、変数の型が実行時に決まるため、関数の引数チェックデバッグで「いま何の型が入っているか」を調べたい場面が頻発します。本記事では各言語での型確認方法をまとめます。

Python

x = 42
y = "hello"
z = [1, 2, 3]

# type() で型オブジェクトを取得
print(type(x))               # <class 'int'>
print(type(y))               # <class 'str'>
print(type(z))               # <class 'list'>

# 名前だけ取り出す
print(type(x).__name__)      # 'int'
print(x.__class__.__name__)  # 'int' (同等)

# 厳密一致 (継承を見ない)
if type(x) is int:
    print("int です")

# isinstance: 継承を考慮 (推奨)
print(isinstance(x, int))            # True
print(isinstance(True, int))         # True (bool は int のサブクラス)
print(isinstance(x, (int, float)))   # 複数型 OK

# クラスでの利用
class Animal: pass
class Dog(Animal): pass

d = Dog()
isinstance(d, Animal)         # True (継承を見る)
type(d) is Animal             # False (厳密一致)

# Python 3.10+ の Pattern Matching
match z:
    case list() if all(isinstance(i, int) for i in z):
        print("整数のリスト")
    case dict():
        print("辞書")
    case _:
        print("その他")

typing による型ヒント (Python 3.5+)

from typing import List, Dict, Optional, Union

def greet(name: str, count: int = 1) -> str:
    return f"Hello, {name}!" * count

def process(items: List[Dict[str, int]]) -> Optional[int]:
    return sum(d.get("score", 0) for d in items) or None

# Python 3.10+ では Union を | で書ける
def parse(value: int | str | None) -> str:
    return str(value)

# 静的解析: mypy / pyright で検証
# $ mypy myscript.py

JavaScript

// typeof でプリミティブ判定 (7 種類の文字列を返す)
typeof 42;             // &quot;number&quot;
typeof &quot;hi&quot;;           // &quot;string&quot;
typeof true;           // &quot;boolean&quot;
typeof undefined;      // &quot;undefined&quot;
typeof Symbol();       // &quot;symbol&quot;
typeof 10n;            // &quot;bigint&quot;
typeof function(){};   // &quot;function&quot;
typeof {};             // &quot;object&quot;
typeof null;           // &quot;object&quot; ← 歴史的バグ
typeof [];             // &quot;object&quot; ← 配列も object

// 配列判定は Array.isArray() を使う
Array.isArray([]);     // true
Array.isArray({});     // false

// null 判定
const v = null;
v === null;            // true (推奨)
typeof v === &quot;object&quot; &amp;&amp; v === null;  // 冗長

// instanceof: コンストラクタチェック
[] instanceof Array;          // true
new Date() instanceof Date;   // true

// Object.prototype.toString.call() で精密な型名
Object.prototype.toString.call([]);          // &quot;[object Array]&quot;
Object.prototype.toString.call(null);        // &quot;[object Null]&quot;
Object.prototype.toString.call(new Date());  // &quot;[object Date]&quot;

// ユーティリティ関数
function getType(x) {
    return Object.prototype.toString.call(x).slice(8, -1).toLowerCase();
}
getType([]);           // &quot;array&quot;
getType(null);         // &quot;null&quot;
getType(new Map());    // &quot;map&quot;

TypeScript

// 静的型でコンパイル時に検証
function add(a: number, b: number): number {
    return a + b;
}

// Type Guard で実行時の絞り込み
function process(value: string | number): string {
    if (typeof value === &quot;string&quot;) {
        return value.toUpperCase();    // ここでは string と確定
    }
    return value.toFixed(2);           // ここでは number と確定
}

// ユーザ定義 Type Guard
function isUser(obj: unknown): obj is { name: string; age: number } {
    return typeof obj === &quot;object&quot; &amp;&amp; obj !== null
        &amp;&amp; &quot;name&quot; in obj &amp;&amp; typeof (obj as any).name === &quot;string&quot;
        &amp;&amp; &quot;age&quot; in obj &amp;&amp; typeof (obj as any).age === &quot;number&quot;;
}

Java

Object x = "Hello";

// getClass: ランタイムの実クラスを取得
Class<?> c = x.getClass();
System.out.println(c.getName());        // java.lang.String
System.out.println(c.getSimpleName());  // String

// instanceof: 型チェック (継承も見る)
if (x instanceof String) {
    String s = (String) x;
    System.out.println(s.length());
}

// Java 16+ Pattern Matching for instanceof
if (x instanceof String s) {
    System.out.println(s.length());      // キャスト不要
}

// Java 21 Switch Pattern Matching
String describe(Object obj) {
    return switch (obj) {
        case Integer i -> "整数: " + i;
        case String s when s.isEmpty() -> "空文字列";
        case String s -> "文字列(" + s.length() + ")";
        case int[] arr -> "int 配列(" + arr.length + ")";
        case null -> "null";
        default -> "その他: " + obj.getClass().getSimpleName();
    };
}

// Class.isInstance() (動的判定)
Class<?> klass = String.class;
boolean ok = klass.isInstance(x);

PHP

$x = 42;
$y = &quot;hello&quot;;
$z = [1, 2, 3];

// gettype: 文字列で型名取得
gettype($x);          // &quot;integer&quot;
gettype($y);          // &quot;string&quot;
gettype($z);          // &quot;array&quot;
gettype(null);        // &quot;NULL&quot;

// is_*() 系 (推奨)
is_int($x);           // true (is_integer も同じ)
is_string($y);        // true
is_array($z);         // true
is_bool($v);
is_float($v);         // is_double, is_real も同じ
is_null($v);
is_numeric(&quot;123&quot;);    // true (文字列でも数値ならtrue)
is_object($obj);
is_callable($fn);

// クラス確認
class User {}
$u = new User();

get_class($u);                   // &quot;User&quot;
$u::class;                       // &quot;User&quot; (PHP 8.0+)
get_parent_class($u);            // 親クラス名 or false

// instanceof
$u instanceof User;              // true

// 名前空間付き
use App\Models\User;
$u instanceof \App\Models\User;  // true

// PHP 8 の型宣言
function greet(string $name, int $count = 1): string {
    return str_repeat(&quot;Hello, $name! &quot;, $count);
}

// Union 型 (PHP 8.0+)
function parseValue(int|string|null $v): string {
    return (string) $v;
}

// readonly プロパティ (PHP 8.1+)
class Point {
    public function __construct(
        public readonly float $x,
        public readonly float $y,
    ) {}
}

主要言語の対応表

機能PythonJavaScriptJavaPHP
型名取得type(x).__name__typeof xx.getClass().getSimpleName()gettype($x)
厳密一致type(x) is inttypeof x === "number"x.getClass() == Integer.classis_int($x)
継承考慮isinstance(x, int)x instanceof Numberx instanceof Integer$x instanceof Foo
配列判定isinstance(x, list)Array.isArray(x)x.getClass().isArray()is_array($x)
null 判定x is Nonex === nullx == nullis_null($x)

デバッガでの型確認

  • VS Code (Python / Node.js / PHP): ブレークポイントで止めて Variables パネルに型と値が表示
  • IntelliJ / PyCharm: 同様、Watches で type(x) を評価可能
  • Chrome DevTools: console の console.dir(x) で詳細プロパティを表示
  • Xdebug (PHP): var_dump($x) でステップ実行中の値・型確認
  • jdb / IntelliJ Debugger (Java): 変数の宣言型と実行時型を別表示

静的解析で実行時チェックを減らす

言語静的解析ツール
Pythonmypy / pyright / Pyre
JavaScriptTypeScript / JSDoc + tsc --checkJs
PHPPHPStan / Psalm
RubyRBS + Steep / Sorbet

これらを CI に組み込むと、実行時の isinstance / typeof チェックは大幅に減らせます。

FAQ

Q: Python で typeisinstance どちらを使うべき?
A: 原則 isinstance。継承を尊重するため。例外的に「特定クラスの厳密一致」が必要なときだけ type(x) is Foo

Q: JavaScript で typeof null === "object" はなぜ?
A: 初期実装の歴史的バグです。後方互換のため修正されないままです。null 判定はx === nullと書きましょう。

Q: PHP で is_int("123") が false なのは?
A: 厳密に「整数型」かを見るからです。「整数として解釈可能」を確認したいなら is_numeric()ctype_digit() を使います。