
Security News
PolinRider: North Korea-Linked Supply Chain Campaign Expands Across Open Source Ecosystems
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.
@wopjs/cast
Advanced tools
Type-safe utilities for filtering and coercing unknown values in TypeScript.
is/to/as) for flexible type filteringnpm add @wopjs/cast
Every type has up to three functions following a consistent naming pattern:
| Pattern | Returns | Use Case |
|---|---|---|
is* | boolean | Type guards for conditional narrowing |
to* | T | undefined | Optional values, composable with Option types |
as* | T | Always 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
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
}
| Function | Description |
|---|---|
isTrue | Returns true if value is exactly true |
toTrue | Returns true or undefined |
asTrue | Returns true or false |
isTruthy | Returns true if Boolean(x) is true |
toTruthy | Returns truthy value or undefined |
isFalsy | Returns true if Boolean(x) is false |
toFalsy | Returns falsy value or undefined |
isBoolean | Returns true if value is true or false |
toBoolean | Returns boolean or undefined |
| Function | Description |
|---|---|
isNumber | Returns true if value is a number (excluding NaN) |
toNumber | Returns number or undefined |
asNumber | Returns number or 0 |
| Function | Description |
|---|---|
isString | Returns true if value is a string |
toString | Returns string or undefined |
asString | Returns string or "" |
isNonEmptyString | Returns true if value is a non-empty string |
toNonEmptyString | Returns non-empty string or undefined |
| Function | Description |
|---|---|
isArray | Type guard for arrays (preserves element types) |
toArray | Returns array or undefined |
asArray | Returns array or [] |
isNonEmptyArray | Type guard for non-empty arrays |
toNonEmptyArray | Returns non-empty array or undefined |
| Function | Description |
|---|---|
isObject | Returns true for objects (including arrays, excluding null) |
toObject | Returns object or undefined |
asObject | Returns object or {} |
isPlainObject | Returns true for plain objects (excluding arrays) |
toPlainObject | Returns plain object or undefined |
asPlainObject | Returns plain object or {} |
isNonEmptyPlainObject | Returns true for objects with at least one key |
toNonEmptyPlainObject | Returns non-empty object or undefined |
isNonEmptyJSONObject | Returns true for objects with non-undefined values |
toNonEmptyJSONObject | Returns JSON object or undefined |
| Function | Description |
|---|---|
isDefined | Returns true if value is not undefined |
toPlainObjectOf | Filter object values by type predicate |
toPlainObjectOfTrue | Filter object to only true values |
print | Safely stringify any value for display |
import * as c from "@wopjs/cast";
// Parsing unknown API response
function parseUser(data: unknown) {
const obj = c.asPlainObject(data);
return {
name: c.asString(obj.name),
age: c.toNumber(obj.age), // undefined if not a number
tags: c.asArray(obj.tags),
settings: c.toNonEmptyPlainObject(obj.settings),
};
}
// Filtering object values
const config = { debug: true, verbose: false, enabled: true };
c.toPlainObjectOfTrue(config); // { debug: true, enabled: true }
// Safe number handling
c.isNumber(NaN); // false (NaN is excluded)
c.asNumber(NaN); // 0 (safe fallback)
c.asNumber("42"); // 0 (not coerced, use parseInt for that)
You can use npm version to bump version.
npm version patch
Push the tag to remote and CI will publish the new version to npm.
git push --follow-tags
If you want to publish the package in CI, you need to set the NPM_TOKEN secrets in GitHub repository settings. See how to create a NPM access token.
FAQs
Type-safe utilities for filtering and coercing unknown values in TypeScript.
The npm package @wopjs/cast receives a total of 116 weekly downloads. As such, @wopjs/cast popularity was classified as not popular.
We found that @wopjs/cast demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

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.

Security News
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.

Security News
Open source attacks are accelerating as AI coding agents pull in dependencies faster, with less human review.

Research
/Security News
Malicious Chrome and Firefox extensions posed as free VPNs while stealing clipboard data through later extension updates.