Skip to content

Commit

Permalink
Improve is.plainObject TypeScript type (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocktimsaikia authored Oct 12, 2020
1 parent bf6bba7 commit 94749db
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ is.primitive = (value: unknown): value is Primitive => is.null_(value) || isPrim
is.integer = (value: unknown): value is number => Number.isInteger(value as number);
is.safeInteger = (value: unknown): value is number => Number.isSafeInteger(value as number);

is.plainObject = <Value = unknown>(value: unknown): value is Record<string, Value> => {
type ObjectKey = string | number | symbol;
is.plainObject = <Value = unknown>(value: unknown): value is Record<ObjectKey, Value> => {
// From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
if (toString.call(value) !== '[object Object]') {
return false;
Expand Down Expand Up @@ -494,7 +495,7 @@ interface Assert {
primitive: (value: unknown) => asserts value is Primitive;
integer: (value: unknown) => asserts value is number;
safeInteger: (value: unknown) => asserts value is number;
plainObject: <Value = unknown>(value: unknown) => asserts value is Record<string, Value>;
plainObject: <Value = unknown>(value: unknown) => asserts value is Record<ObjectKey, Value>;
typedArray: (value: unknown) => asserts value is TypedArray;
arrayLike: <T = unknown>(value: unknown) => asserts value is ArrayLike<T>;
domElement: (value: unknown) => asserts value is HTMLElement;
Expand Down Expand Up @@ -591,7 +592,7 @@ export const assert: Assert = {
primitive: (value: unknown): asserts value is Primitive => assertType(is.primitive(value), AssertionTypeDescription.primitive, value),
integer: (value: unknown): asserts value is number => assertType(is.integer(value), AssertionTypeDescription.integer, value),
safeInteger: (value: unknown): asserts value is number => assertType(is.safeInteger(value), AssertionTypeDescription.safeInteger, value),
plainObject: <Value = unknown>(value: unknown): asserts value is Record<string, Value> => assertType(is.plainObject(value), AssertionTypeDescription.plainObject, value),
plainObject: <Value = unknown>(value: unknown): asserts value is Record<ObjectKey, Value> => assertType(is.plainObject(value), AssertionTypeDescription.plainObject, value),
typedArray: (value: unknown): asserts value is TypedArray => assertType(is.typedArray(value), AssertionTypeDescription.typedArray, value),
arrayLike: <T = unknown>(value: unknown): asserts value is ArrayLike<T> => assertType(is.arrayLike(value), AssertionTypeDescription.arrayLike, value),
domElement: (value: unknown): asserts value is HTMLElement => assertType(is.domElement(value), AssertionTypeDescription.domElement, value),
Expand Down

0 comments on commit 94749db

Please sign in to comment.