From b909111f10a8bdf078f3ef46ecee80f02b32bab6 Mon Sep 17 00:00:00 2001 From: Bjorn Stromberg Date: Sun, 7 Jun 2020 15:06:30 +0900 Subject: [PATCH] Narrow types for isOfType and consolidate typeof switch in is --- source/index.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/source/index.ts b/source/index.ts index 8d83e93..78c8cf4 100644 --- a/source/index.ts +++ b/source/index.ts @@ -98,7 +98,8 @@ function isPrimitiveTypeName(name: any): name is PrimitiveTypeName { export type TypeName = ObjectTypeName | PrimitiveTypeName; const {toString} = Object.prototype; -const isOfType = (type: string) => (value: unknown): value is T => typeof value === type; +// eslint-disable-next-line @typescript-eslint/ban-types +const isOfType = (type: string) => (value: unknown): value is T => typeof value === type; const getObjectType = (value: unknown): ObjectTypeName | undefined => { const objectTypeName = toString.call(value).slice(8, -1); @@ -117,13 +118,8 @@ const getObjectType = (value: unknown): ObjectTypeName | undefined => { const isObjectOfType = (type: ObjectTypeName) => (value: unknown): value is T => getObjectType(value) === type; function is(value: unknown): TypeName { - switch (value) { - case null: - return 'null'; - case true: - case false: - return 'boolean'; - default: + if (value === null) { + return 'null'; } switch (typeof value) { @@ -133,6 +129,10 @@ function is(value: unknown): TypeName { return 'string'; case 'number': return 'number'; + case 'boolean': + return 'boolean'; + case 'function': + return 'Function'; case 'bigint': return 'bigint'; case 'symbol': @@ -140,10 +140,6 @@ function is(value: unknown): TypeName { default: } - if (is.function_(value)) { - return 'Function'; - } - if (is.observable(value)) { return 'Observable'; }