The Inspector is a set of utilities that help you strictly check the variable types in your code, includes the type guard.
The isString
function is a type guard that helps you to check if a variable is a string.
import { isString } from '@beerush/utilities';
let value: string | null = 'Hello, World!';
if (isString(value)) {
// Even though the type of value is string | null, isString will infer the type of value as string.
console.log(value.replace('World', 'Internet')); // Hello, Internet!
}
The isNumber
function is a type guard that helps you to check if a variable is a number. It will return true
if the
variable is a number and not NaN
, and false
otherwise.
import { isNumber } from '@beerush/utilities';
let value: number | null = 10;
if (isNumber(value)) {
// Even though the type of value is number | null, isNumber will infer the type of value as number.
console.log(value + 10); // 20
}
The isInt
function is a type guard that helps you to check if a variable is an integer.
import { isInt } from '@beerush/utilities';
const int = 10;
const float = 10.5;
if (isInt(int)) {
console.log('This is an integer.');
}
if (isInt(float)) {
console.log('This is an integer.');
} else {
console.log('This is not an integer.');
}
The isFloat
function is a type guard that helps you to check if a variable is a float.
import { isFloat } from '@beerush/utilities';
const int = 10;
const float = 10.5;
if (isFloat(int)) {
console.log('This is a float.');
} else {
console.log('This is not a float.');
}
if (isFloat(float)) {
console.log('This is a float.');
}
The isEven
function is a type guard that helps you to check if a variable is an even number.
import { isEven } from '@beerush/utilities';
const even = 10;
if (isEven(even)) {
console.log('This is an even number.');
}
The isOdd
function is a type guard that helps you to check if a variable is an odd number.
import { isOdd } from '@beerush/utilities';
const odd = 11;
if (isOdd(odd)) {
console.log('This is an odd number.');
}
The isBoolean
function is a type guard that helps you to check if a variable is a boolean.
import { isBoolean } from '@beerush/utilities';
const bool = true;
if (isBoolean(bool)) {
console.log('This is a boolean.');
}
The isBooleanString
function is a type guard that helps you to check if a variable is a boolean string.
import { isBooleanString } from '@beerush/utilities';
const bool = 'true';
if (isBooleanString(bool)) {
console.log('This is a boolean string.');
}
The isObject
function is a type guard that helps you to check if a variable is a key-value object.
import { isObject } from '@beerush/utilities';
const obj = {};
const arr = [];
if (isObject(obj)) {
console.log('This is an object.');
}
if (isObject(arr)) {
console.log('This is an object.');
} else {
console.log('This is not an object.'); // Output
}
You can also check if a variable is an object with a specific structure by using the generic version of isObject
.
import { isObject } from '@beerush/utilities';
type User = { id: number; name: string };
let user = { id: 1, name: 'John Doe' };
if (isObject<User>(user)) {
console.log(`Hello, ${ user.name }!`); // Hello, John Doe!
}
The isObjectLike
function is a type guard that helps you to check if a variable is an object-like, such as an instance
of a class.
import { isObjectLike } from '@beerush/utilities';
class User {
constructor(public id: number, public name: string) { }
}
const obj = {};
const arr = [];
const user = new User(1, 'John Doe');
if (isObjectLike(obj)) {
console.log('This is an object-like.');
}
if (isObjectLike(user)) {
console.log('This is an object-like.');
}
if (isObjectLike(arr)) {
console.log('This is an object-like.');
} else {
console.log('This is not an object-like.'); // Output
}
The isArray
function is a type guard that helps you to check if a variable is an array.
import { isArray } from '@beerush/utilities';
const obj = {};
const arr = [];
if (isArray(obj)) {
console.log('This is an array.');
} else {
console.log('This is not an array.'); // Output
}
if (isArray(arr)) {
console.log('This is an array.');
}
The isFunction
function is a type guard that helps you to check if a variable is a function.
import { isFunction } from '@beerush/utilities';
const func = () => { };
if (isFunction(func)) {
console.log('This is a function.');
}
The isDate
function is a type guard that helps you to check if a variable is a date.
import { isDate } from '@beerush/utilities';
const date = new Date();
if (isDate(date)) {
console.log('This is a date.');
}
The isDateString
function is a type guard that helps you to check if a variable is a date string.
import { isDateString } from '@beerush/utilities';
const date = '2024-05-09';
if (isDateString(date)) {
console.log('This is a date string.');
}
The isRegExp
function is a type guard that helps you to check if a variable is a regular expression.
import { isRegExp } from '@beerush/utilities';
const regex = /Hello, World/;
if (isRegExp(regex)) {
console.log('This is a regular expression.');
}
The isError
function is a type guard that helps you to check if a variable is an error.
import { isError } from '@beerush/utilities';
const error = new Error('Something went wrong!');
if (isError(error)) {
console.log('This is an error.');
}
The isNullish
function is a type guard that helps you to check if a variable is null
or undefined
.
import { isNullish } from '@beerush/utilities';
let value: string | null = null;
let other: number | null = NaN;
if (isNullish(value)) {
console.log('This is null, NaN, or undefined.');
}
if (isNullish(other)) {
console.log('This is null, NaN, or undefined.');
}
The isTruthy
function is a type guard that helps you to check if a variable is truthy.
import { isTruthy } from '@beerush/utilities';
let value: string | null = 'Hello, World!';
if (isTruthy(value)) {
console.log('This is a truthy value.');
}
The isFalsy
function is a type guard that helps you to check if a variable is falsy, but excluding 0
, negative
number, and empty string.
import { isFalsy } from '@beerush/utilities';
let value: string | null = null;
const foo = false;
const bar = '';
const baz = -1;
if (isFalsy(value)) {
console.log('This is a falsy value.');
}
if (isFalsy(foo)) {
console.log('This is a falsy value.');
}
if (isFalsy(bar)) {
console.log('This is not a falsy value.');
}
if (isFalsy(baz)) {
console.log('This is not a falsy value.');
}
The isPositive
function is a type guard that helps you to check if a variable is a positive number.
import { isPositive } from '@beerush/utilities';
const positive = 10;
const negative = -10;
if (isPositive(positive)) {
console.log('This is a positive number.');
}
if (isPositive(negative)) {
console.log('This is not a positive number.');
}
The isEmpty
function is a type guard that helps you to check if a variable is empty. The function will return true
- If the variable is a
nullish
value. - if the variable is a zero number.
- If the variable is an empty string.
- If the variable is an empty array.
- If the variable is an empty object.
- If the variable is an empty map.
- If the variable is an empty set.
import { isEmpty } from '@beerush/utilities';
let empty: string | null = null;
let str = '';
let arr = [];
let obj = {};
let map = new Map();
let set = new Set();
if (isEmpty(empty)) {
console.log('This is an empty value.');
}
if (isEmpty(str)) {
console.log('This is an empty string.');
}
if (isEmpty(arr)) {
console.log('This is an empty array.');
}
if (isEmpty(obj)) {
console.log('This is an empty object.');
}
if (isEmpty(map)) {
console.log('This is an empty map.');
}
if (isEmpty(set)) {
console.log('This is an empty set.');
}
The typeOf
function is a utility function that helps you to check the type of a variable. Unlike the typeof
operator, the typeOf
function will return the type of the variable using the toString.call
method.
For example, typeOf([])
will return array
instead of object
.
import { typeOf } from '@beerush/utilities';
console.log(typeOf({})); // object
console.log(typeOf([])); // array
console.log(typeOf(null)); // null
console.log(typeOf('')); // string
console.log(typeOf(0)); // number