Skip to content

Commit

Permalink
directInstanceOf: Fix handling of undefined and null
Browse files Browse the repository at this point in the history
Fixes #199
  • Loading branch information
sindresorhus committed Oct 15, 2023
1 parent 9d6c91e commit e7e2213
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ export function isDate(value: unknown): value is Date {
}

export function isDirectInstanceOf<T>(instance: unknown, class_: Class<T>): instance is T {
if (instance === undefined || instance === null) {
return false;
}

return Object.getPrototypeOf(instance) === class_.prototype;
}

Expand Down
3 changes: 3 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit e7e2213

Please sign in to comment.