From 7a8a9593350020493aab1b762bdb9b431efa0932 Mon Sep 17 00:00:00 2001 From: Matteo Sacchetto <56300116+matteosacchetto@users.noreply.github.com> Date: Fri, 14 Oct 2022 20:06:36 +0200 Subject: [PATCH] feat: add type guards (#103) * feat: add type guards add type guards to all isN functions * fix: use unknown[] instead of any[] in isArray type guard --- src/typed.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/typed.ts b/src/typed.ts index 38a48618..d36c6ede 100644 --- a/src/typed.ts +++ b/src/typed.ts @@ -1,32 +1,32 @@ -export const isSymbol = (value: any) => { +export const isSymbol = (value: any): value is symbol => { return !!value && value.constructor === Symbol } -export const isArray = (value: any) => { +export const isArray = (value: any): value is unknown[] => { return !!value && value.constructor === Array } -export const isObject = (value: any) => { +export const isObject = (value: any): value is object => { return !!value && value.constructor === Object } -export const isFunction = (value: any) => { +export const isFunction = (value: any): value is Function => { return !!(value && value.constructor && value.call && value.apply) } -export const isString = (value: any) => { +export const isString = (value: any): value is string => { return typeof value === 'string' || value instanceof String } -export const isInt = (value: any) => { +export const isInt = (value: any): value is number => { return isNumber(value) && value % 1 === 0 } -export const isFloat = (value: any) => { +export const isFloat = (value: any): value is number => { return isNumber(value) && value % 1 !== 0 } -export const isNumber = (value: any) => { +export const isNumber = (value: any): value is number => { try { return Number(value) === value } catch { @@ -34,15 +34,15 @@ export const isNumber = (value: any) => { } } -export const isDate = (value: any) => { +export const isDate = (value: any): value is Date => { return Object.prototype.toString.call(value) === "[object Date]" } export const isEmpty = (value: any) => { if (value === true || value === false) return true if (value === null || value === undefined) return true - if (isNumber(value)) return parseInt(value) === 0 - if (isDate(value)) return isNaN(value) + if (isNumber(value)) return value === 0 + if (isDate(value)) return isNaN(value.getTime()) if (isFunction(value)) return false if (isSymbol(value)) return false const length = (value as any).length