JavaScript and TypeScript type checking utility functions providing precise type validation with automatic type narrowing.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Array validation functions providing comprehensive array detection including empty/full array checks and general array validation.
Returns whether the payload is an array.
/**
* Returns whether the payload is an array
* @param payload - Value to check
* @returns Type guard indicating if payload is array
*/
function isArray(payload: unknown): payload is unknown[];Usage Examples:
import { isArray } from "is-what";
isArray([]); // true
isArray([1, 2, 3]); // true
isArray(["a", "b", "c"]); // true
isArray(new Array()); // true
isArray({}); // false
isArray("[]"); // false
isArray(null); // false
if (isArray(data)) {
// TypeScript knows data is an array
console.log(`Array has ${data.length} items`);
data.forEach(item => console.log(item));
}Returns whether the payload is an empty array (length === 0).
/**
* Returns whether the payload is an empty array
* @param payload - Value to check
* @returns Type guard indicating if payload is empty array
*/
function isEmptyArray(payload: unknown): payload is [];Usage Examples:
import { isEmptyArray } from "is-what";
isEmptyArray([]); // true
isEmptyArray([1]); // false
isEmptyArray([null]); // false
isEmptyArray({}); // false
isEmptyArray(""); // false
// Useful for validation
function processData(input: unknown) {
if (isEmptyArray(input)) {
console.log("No data to process");
return;
}
// Continue processing...
}
// Type narrowing example
if (isEmptyArray(list)) {
// TypeScript knows list is [] (empty array)
console.log("Empty array detected");
} else if (isArray(list)) {
// TypeScript knows list is unknown[] with length > 0
console.log(`Processing ${list.length} items`);
}Returns whether the payload is a non-empty array (has at least one element).
/**
* Returns whether the payload is an array with at least one element
* @param payload - Value to check
* @returns Type guard indicating if payload is non-empty array
*/
function isFullArray(payload: unknown): payload is unknown[];Usage Examples:
import { isFullArray } from "is-what";
isFullArray([1, 2, 3]); // true
isFullArray(["item"]); // true
isFullArray([null]); // true (contains one element, even if null)
isFullArray([]); // false
isFullArray("full"); // false
isFullArray({}); // false
// Practical usage
function getFirstItem(input: unknown) {
if (isFullArray(input)) {
return input[0]; // Safe to access first element
}
return null;
}
// Validation pattern
function validateRequiredList(list: unknown, fieldName: string) {
if (!isFullArray(list)) {
throw new Error(`${fieldName} must be a non-empty array`);
}
// TypeScript knows list is unknown[] with at least one element
return list;
}import { isArray, isEmptyArray, isFullArray } from "is-what";
function analyzeArray(value: unknown) {
if (!isArray(value)) {
return "Not an array";
}
if (isEmptyArray(value)) {
return "Empty array";
}
if (isFullArray(value)) {
return `Array with ${value.length} items`;
}
// This shouldn't happen, but TypeScript doesn't know that
return "Unknown array state";
}
// Array processing pipeline
function processArrayData(input: unknown) {
// First check if it's an array at all
if (!isArray(input)) {
throw new Error("Input must be an array");
}
// Handle empty array case
if (isEmptyArray(input)) {
console.log("No items to process");
return [];
}
// Process non-empty array
console.log(`Processing ${input.length} items`);
return input.map((item, index) => ({
index,
value: item,
processed: true
}));
}
// Type-safe array operations
function safeArrayOperations(data: unknown) {
if (isArray(data)) {
// All array methods are available
const doubled = data.map(x => x);
const filtered = data.filter(x => x != null);
if (isEmptyArray(data)) {
console.log("Cannot perform operations on empty array");
return null;
}
if (isFullArray(data)) {
// Safe to access elements
const first = data[0];
const last = data[data.length - 1];
return { first, last, length: data.length };
}
}
return null;
}
// Example usage
analyzeArray([]); // "Empty array"
analyzeArray([1, 2, 3]); // "Array with 3 items"
analyzeArray("not array"); // "Not an array"
const result = processArrayData([1, 2, 3]);
// Logs: "Processing 3 items"
// Returns: [
// { index: 0, value: 1, processed: true },
// { index: 1, value: 2, processed: true },
// { index: 2, value: 3, processed: true }
// ]