From 002492382c0415e60493d9ae3f28fe6a2b845602 Mon Sep 17 00:00:00 2001 From: Alex Iglesias Date: Thu, 27 Oct 2022 01:32:49 +0200 Subject: [PATCH] Added primitive type guards --- .changeset/pretty-drinks-reflect.md | 5 +++++ src/type-guards/index.ts | 1 + src/type-guards/primitives.ts | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 .changeset/pretty-drinks-reflect.md create mode 100644 src/type-guards/primitives.ts diff --git a/.changeset/pretty-drinks-reflect.md b/.changeset/pretty-drinks-reflect.md new file mode 100644 index 0000000..386fad0 --- /dev/null +++ b/.changeset/pretty-drinks-reflect.md @@ -0,0 +1,5 @@ +--- +'@finsweet/ts-utils': minor +--- + +Added primitive type guards diff --git a/src/type-guards/index.ts b/src/type-guards/index.ts index ca8003c..17d68c3 100644 --- a/src/type-guards/index.ts +++ b/src/type-guards/index.ts @@ -1,3 +1,4 @@ export { isFormField } from './isFormField'; export { isKeyOf } from './isKeyOf'; export { isNotEmpty } from './isNotEmpty'; +export * from './primitives'; diff --git a/src/type-guards/primitives.ts b/src/type-guards/primitives.ts new file mode 100644 index 0000000..25c43e2 --- /dev/null +++ b/src/type-guards/primitives.ts @@ -0,0 +1,19 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/ban-types */ +export const isString = (value: unknown): value is string => typeof value === 'string'; + +export const isNumber = (value: unknown): value is number => typeof value === 'number'; + +export const isBigint = (value: unknown): value is bigint => typeof value === 'bigint'; + +export const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean'; + +export const isSymbol = (value: unknown): value is symbol => typeof value === 'symbol'; + +export const isUndefined = (value: unknown): value is undefined => value === undefined; + +export const isNull = (value: unknown): value is null => value === null; + +export const isFunction = (value: unknown): value is Function => typeof value === 'function'; + +export const isObject = (value: unknown): value is Record => value !== null && typeof value === 'object';