type-guards-ts
v1.0.4
Published
A library for TypeScript type guards
Readme
Type Guards TS
type-guards-ts is a TypeScript library providing a collection of type guard functions to help you perform runtime type checking and type narrowing. This library includes functions to check if a value is of a certain type (e.g., isBoolean) as well as functions to check if a value is not of a certain type (e.g., isNotBoolean).
Index
Installation
To install the package, run the following command:
npm install type-guards-tsHow to Use
To use the type guards provided by type-guards-ts, you can import the required functions from the package and use them in your TypeScript code. Here’s a quick example:
import { isBoolean } from "type-guards-ts";
const value: unknown = true;
if (isBoolean(value)) {
// TypeScript now knows that `value` is a boolean
console.log("The value is a boolean.");
} else {
console.log("The value is not a boolean.");
}Available Type Guards
isBigIntisBooleanisFunctionisNullisNumberisObjectisStringisSymbolisUndefinedisArrayisDateisNotBigIntisNotBooleanisNotFunctionisNotNullisNotNumberisNotObjectisNotStringisNotSymbolisNotUndefinedisNotArrayisNotDate
How to Use Each Type Guard
isBigInt
Checks if a value is a BigInt.
import { isBigInt } from "type-guards-ts";
const value: unknown = BigInt(123);
if (isBigInt(value)) {
console.log("The value is a BigInt.");
} else {
console.log("The value is not a BigInt.");
}isBoolean
Checks if a value is a boolean.
import { isBoolean } from "type-guards-ts";
const value: unknown = true;
if (isBoolean(value)) {
console.log("The value is a boolean.");
} else {
console.log("The value is not a boolean.");
}isFunction
Checks if a value is a function.
import { isFunction } from "type-guards-ts";
const value: unknown = () => {};
if (isFunction(value)) {
console.log("The value is a function.");
} else {
console.log("The value is not a function.");
}isNull
Checks if a value is null.
import { isNull } from "type-guards-ts";
const value: unknown = null;
if (isNull(value)) {
console.log("The value is null.");
} else {
console.log("The value is not null.");
}isNumber
Checks if a value is a number.
import { isNumber } from "type-guards-ts";
const value: unknown = 42;
if (isNumber(value)) {
console.log("The value is a number.");
} else {
console.log("The value is not a number.");
}isObject
Checks if a value is an object.
import { isObject } from "type-guards-ts";
const value: unknown = { key: "value" };
if (isObject(value)) {
console.log("The value is an object.");
} else {
console.log("The value is not an object.");
}isString
Checks if a value is a string.
import { isString } from "type-guards-ts";
const value: unknown = "Hello, world!";
if (isString(value)) {
console.log("The value is a string.");
} else {
console.log("The value is not a string.");
}isSymbol
Checks if a value is a symbol.
import { isSymbol } from "type-guards-ts";
const value: unknown = Symbol("symbol");
if (isSymbol(value)) {
console.log("The value is a symbol.");
} else {
console.log("The value is not a symbol.");
}isUndefined
Checks if a value is undefined.
import { isUndefined } from "type-guards-ts";
const value: unknown = undefined;
if (isUndefined(value)) {
console.log("The value is undefined.");
} else {
console.log("The value is not undefined.");
}isArray
Checks if a value is an array.
import { isArray } from "type-guards-ts";
const value: unknown = [];
if (isArray(value)) {
console.log("The value is an array.");
} else {
console.log("The value is not array.");
}Below is an example of documentation in Markdown format that teaches how to use the isDate and isNotDate type guards.
isDate
Description:
Checks whether the provided value is a Date object.
This function verifies if the given value is a Date by first checking whether it is an instance of Date. It also uses Object.prototype.toString to handle cases where the Date object might come from a different execution context.
Usage Example:
import { isDate } from "type-guards-ts";
const value: unknown = new Date();
if (isDate(value)) {
console.log("The value is a Date:", value);
} else {
console.log("The value is not a Date.");
}isNotBigInt
Checks if a value is not a BigInt.
import { isNotBigInt } from "type-guards-ts";
const value: unknown = 123;
if (isNotBigInt(value)) {
console.log("The value is not a BigInt.");
} else {
console.log("The value is unknown.");
}isNotBoolean
Checks if a value is not a boolean.
import { isNotBoolean } from "type-guards-ts";
const value: unknown = "true";
if (isNotBoolean(value)) {
console.log("The value is not a boolean.");
} else {
console.log("The value is unknown.");
}isNotFunction
Checks if a value is not a function.
import { isNotFunction } from "type-guards-ts";
const value: unknown = "function";
if (isNotFunction(value)) {
console.log("The value is not a function.");
} else {
console.log("The value is unknown.");
}isNotNull
Checks if a value is not null.
import { isNotNull } from "type-guards-ts";
const value: unknown = "not null";
if (isNotNull(value)) {
console.log("The value is not null.");
} else {
console.log("The value is unknown.");
}isNotNumber
Checks if a value is not a number.
import { isNotNumber } from "type-guards-ts";
const value: unknown = "42";
if (isNotNumber(value)) {
console.log("The value is not a number.");
} else {
console.log("The value is unknown.");
}isNotObject
Checks if a value is not an object.
import { isNotObject } from "type-guards-ts";
const value: unknown = "object";
if (isNotObject(value)) {
console.log("The value is not an object.");
} else {
console.log("The value is unknown.");
}isNotString
Checks if a value is not a string.
import { isNotString } from "type-guards-ts";
const value: unknown = 123;
if (isNotString(value)) {
console.log("The value is not a string.");
} else {
console.log("The value is unknown.");
}isNotSymbol
Checks if a value is not a symbol.
import { isNotSymbol } from "type-guards-ts";
const value: unknown = "symbol";
if (isNotSymbol(value)) {
console.log("The value is not a symbol.");
} else {
console.log("The value is unknown.");
}isNotUndefined
Checks if a value is not undefined.
import { isNotUndefined } from "type-guards-ts";
const value: unknown = "defined";
if (isNotUndefined(value)) {
console.log("The value is not undefined.");
} else {
console.log("The value is unknown.");
}isNotArray
Checks if a value is not an array.
import { isNotArray } from "type-guards-ts";
const value: unknown = "not array";
if (isNotArray(value)) {
console.log("The value is not an array.");
} else {
console.log("The value is not unknown.");
}isNotDate
Description:
Determines if the provided value is not a Date object.
This type guard leverages the isDate helper function to check if a value is not a Date. If the value is not a Date, the function returns true and narrows the type accordingly.
Usage Example:
import { isNotDate } from "type-guards-ts";
const value: unknown = "2023-08-24"; // Example value that is not a Date
if (isNotDate(value)) {
console.log("The value is not a Date:", value);
} else {
console.log("The value is a Date.");
}Future Releases
Currently, this package focuses on type guards for primitive types. However, we're actively working on expanding the library to include type guards for non-primitive types. Stay tuned for future releases where you'll find new and enhanced functionalities!
Keep your code safe and type-safe with type-guards-ts! 🚀
