From e7e2213e9100a4212caf28c4af49efdaa9c83354 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 15 Oct 2023 16:01:29 +0700 Subject: [PATCH] `directInstanceOf`: Fix handling of `undefined` and `null` Fixes #199 --- source/index.ts | 4 ++++ test/test.ts | 3 +++ 2 files changed, 7 insertions(+) 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 => {