TypeError
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
TypeError
オブジェクトは、演算が実行できなくなった時の新しいエラーを表します。特に値が期待された型ではなかった場合です (ただし、それに限りません)。
TypeError
は以下のような場合に発生します。
- 関数に渡されたオペランドや引数が、その演算子や関数で期待された型と互換性がなかった場合
- 変更できない値を変更しようとした場合
- 適切ではない方法で値を使用しようとした場合
コンストラクター
TypeError()
新しい
TypeError
オブジェクトを生成します。
インスタンスプロパティ
TypeError.prototype.message
エラーメッセージです。 ECMA-262 において
TypeError
は自身のmessage
プロパティを提供するべきとされていますが、SpiderMonkey ではError.prototype.message
を継承しています。TypeError.prototype.name
エラー名です。
Error
から継承しています。TypeError.prototype.fileName
このエラーが発生したファイルのパスです。
Error
から継承しています。TypeError.prototype.lineNumber
このエラーが発生したファイル内の行番号です。
Error
から継承しています。TypeError.prototype.columnNumber
このエラーが発生した行内の桁番号です。
Error
から継承しています。TypeError.prototype.stack
スタックトレースです。
Error
から継承しています。
例
TypeError のキャッチ
js
try { null.f();} catch (e) { console.log(e instanceof TypeError); // true console.log(e.message); // "null has no properties" console.log(e.name); // "TypeError" console.log(e.fileName); // "Scratchpad/1" console.log(e.lineNumber); // 2 console.log(e.columnNumber); // 2 console.log(e.stack); // "@Scratchpad/2:2:3\n"}
TypeError の生成
js
try { throw new TypeError("Hello", "someFile.js", 10);} catch (e) { console.log(e instanceof TypeError); // true console.log(e.message); // "Hello" console.log(e.name); // "TypeError" console.log(e.fileName); // "someFile.js" console.log(e.lineNumber); // 10 console.log(e.columnNumber); // 0 console.log(e.stack); // "@Scratchpad/2:2:9\n"}
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-native-error-types-used-in-this-standard-typeerror |