diff --git a/source/index.ts b/source/index.ts index 782a067..848af80 100644 --- a/source/index.ts +++ b/source/index.ts @@ -413,6 +413,10 @@ export function isDate(value: unknown): value is Date { } export function isDirectInstanceOf(instance: unknown, class_: Class): instance is T { + if (instance === undefined || instance === null) { + return false; + } + return Object.getPrototypeOf(instance) === class_.prototype; } diff --git a/test/test.ts b/test/test.ts index 8608a25..bbf7b57 100644 --- a/test/test.ts +++ b/test/test.ts @@ -986,6 +986,9 @@ test('is.directInstanceOf', t => { t.throws(() => { assert.directInstanceOf(errorSubclass, Error); }); + + t.false(is.directInstanceOf(undefined, Error)); + t.false(is.directInstanceOf(null, Error)); }); test('is.urlInstance', t => {