Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@wopjs/cast

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wopjs/cast

Type-safe utilities for filtering and coercing unknown values in TypeScript.

latest
Source
npmnpm
Version
0.1.16
Version published
Weekly downloads
248
14.81%
Maintainers
2
Weekly downloads
 
Created
Source

@wopjs/cast

Docs Build Status npm-version Coverage Status minified-size

Type-safe utilities for filtering and coercing unknown values in TypeScript.

Features

  • Three-tier API pattern (is/to/as) for flexible type filtering
  • Strong TypeScript type narrowing that preserves complex types (unions, tuples, readonly arrays)
  • Zero dependencies and tree-shakeable
  • Safe edge case handling (NaN, null, undefined, circular references)

Install

npm add @wopjs/cast

API Pattern

Every type has up to three functions following a consistent naming pattern:

PatternReturnsUse Case
is*booleanType guards for conditional narrowing
to*T | undefinedOptional values, composable with Option types
as*TAlways returns valid value with fallback
import { isNumber, toNumber, asNumber } from "@wopjs/cast";

// is* - Type guard for conditionals
if (isNumber(value)) {
  value; // type narrowed to number
}

// to* - Returns undefined for invalid input
const num = toNumber(input); // number | undefined

// as* - Always returns a number (0 as fallback)
const safeNum = asNumber(input); // number

Type Narrowing

The library preserves and correctly narrows complex TypeScript types:

import { isArray, toArray, isTruthy } from "@wopjs/cast";

// Preserves array element types
const arr: string[] = ["a", "b"];
if (isArray(arr)) {
  arr; // still string[], not unknown[]
}

// Extracts from unions
const value: string | string[] = getData();
if (isArray(value)) {
  value; // narrowed to string[]
}

// Preserves tuples and readonly arrays
const tuple: [string, number] = ["a", 1];
const result = toArray(tuple); // [string, number] | undefined

// Excludes falsy types
const val: string | null | undefined = "hello";
if (isTruthy(val)) {
  val; // narrowed to string
}

Available Functions

Booleans

FunctionDescription
isTrueReturns true if value is exactly true
toTrueReturns true or undefined
asTrueReturns true or false
isTruthyReturns true if Boolean(x) is true
toTruthyReturns truthy value or undefined
isFalsyReturns true if Boolean(x) is false
toFalsyReturns falsy value or undefined
isBooleanReturns true if value is true or false
toBooleanReturns boolean or undefined

Numbers

FunctionDescription
isNumberReturns true if value is a number (excluding NaN)
toNumberReturns number or undefined
asNumberReturns number or 0

Strings

FunctionDescription
isStringReturns true if value is a string
toStringReturns string or undefined
asStringReturns string or ""
isNonEmptyStringReturns true if value is a non-empty string
toNonEmptyStringReturns non-empty string or undefined

Arrays

FunctionDescription
isArrayType guard for arrays (preserves element types)
toArrayReturns array or undefined
asArrayReturns array or []
isNonEmptyArrayType guard for non-empty arrays
toNonEmptyArrayReturns non-empty array or undefined

Objects

FunctionDescription
isObjectReturns true for objects (including arrays, excluding null)
toObjectReturns object or undefined
asObjectReturns object or {}
isPlainObjectReturns true for plain objects (excluding arrays)
toPlainObjectReturns plain object or undefined
asPlainObjectReturns plain object or {}
isNonEmptyPlainObjectReturns true for objects with at least one key
toNonEmptyPlainObjectReturns non-empty object or undefined
isNonEmptyJSONObjectReturns true for objects with non-undefined values
toNonEmptyJSONObjectReturns JSON object or undefined

Utilities

FunctionDescription
isDefinedReturns true if value is not undefined
toPlainObjectOfFilter object values by type predicate
toPlainObjectOfTrueFilter object to only true values
printSafely stringify any value for display

Return Helpers

Constant functions useful for callbacks and default values:

FunctionReturns
noopvoid
returnsUndefinedundefined
returnsNullnull
returnsFalsefalse
returnsTruetrue
returnsEmptyString""

Usage Example

import {
  asPlainObject,
  asString,
  toNumber,
  asArray,
  toNonEmptyPlainObject,
  toPlainObjectOfTrue,
  isNumber,
  asNumber,
} from "@wopjs/cast";

// Parsing unknown API response
function parseUser(data: unknown) {
  const obj = asPlainObject(data);
  return {
    name: asString(obj.name),
    age: toNumber(obj.age), // undefined if not a number
    tags: asArray(obj.tags),
    settings: toNonEmptyPlainObject(obj.settings),
  };
}

// Filtering object values
const config = { debug: true, verbose: false, enabled: true };
toPlainObjectOfTrue(config); // { debug: true, enabled: true }

// Safe number handling
isNumber(NaN); // false (NaN is excluded)
asNumber(NaN); // 0 (safe fallback)
asNumber("42"); // 0 (not coerced, use parseInt for that)

Keywords

typescript

FAQs

Package last updated on 07 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts