TypeError: "x" is not a non-null object

JavaScript の例外 "is not a non-null object" は、ある場所でオブジェクトが期待されているのに提供されなかった場合に発生します。 null はオブジェクトではなく、動作しません。

エラーメッセージ

TypeError: Invalid descriptor for property {x} (Edge)
TypeError: "x" is not a non-null object (Firefox)
TypeError: Property description must be an object: "x" (Chrome)
TypeError: Invalid value used in weak set (Chrome)

エラーの種類

エラーの原因

ある場所でオブジェクトが期待されていますが、提供されませんでした。 null はオブジェクトではなく、動作しません。与えられた状況で適切なオブジェクトを提供しなければなりません。

プロパティ記述子が求められている場合

Object.create() メソッドや Object.defineProperty() メソッド、Object.defineProperties() メソッドを使用するとき、省略可能な記述子の引数として、プロパティ記述子オブジェクトが想定されます。 (ただの数値など) オブジェクト以外のものを提供すると、エラーが発生します。

Object.defineProperty({}, 'key', 1);
// TypeError: 1 is not a non-null object

Object.defineProperty({}, 'key', null);
// TypeError: null is not a non-null object

有効なプロパティ記述子はこのようになります。

Object.defineProperty({}, 'key', { value: 'foo', writable: false });

WeakMap および WeakSet オブジェクトにはオブジェクトキーが必要

WeakMap および WeakSet オブジェクトはオブジェクトをキーとして保持します。そのほかの型をキーとして使用できません。

var ws = new WeakSet();
ws.add('foo');
// TypeError: "foo" is not a non-null object

代わりにオブジェクトを使用してください。

ws.add({foo: 'bar'});
ws.add(window);

関連項目