@apexo/type

中文文档
0.0.3发布于 2025/11/30MITTypeScriptESM + CJS零依赖近 30 天下载 2772

@apexo/type is a small, zero-dependency library that gives you easy and reliable type checks in JavaScript and TypeScript.

原始使用文档
文档目录24 节
  1. Why would I want that?
  2. How little is it?
  3. How do I install it?
  4. How do I use it?
  5. Basic Type Guards
  6. Validating Values
  7. Advanced Utilities
  8. Loading the library
  9. ES Modules (Recommended)
  10. CommonJS
  11. TypeScript
  12. Are there TypeScript definitions?
  13. Where can I use it?
  14. API Reference
  15. Primitives
  16. Objects & Structures
  17. Advanced Types
  18. Empty Checks
  19. Number Validators
  20. Date Validators
  21. Utilities
  22. Architecture
  23. License
  24. Repository

@apexo/type

Ever spent hours writing validation code, only to realize you're doing the same thing in every function? Or maybe you've let AI generate it for you, and now your bundle is bloated with duplicate boilerplate?

@apexo/type is a small library that does all your type checking for you. Why do you need it? Because JavaScript's type checking is weird:

  • typeof null says it's an "object" (but it's not!)
  • typeof [] says it's an "object" (arrays need different checks)
  • typeof NaN says it's a "number" (even though NaN means "Not a Number")
  • No easy way to check for empty values or loops in objects

You could write all the checks yourself:

function processUser(data) {
    if (data === null || data === undefined) {
        throw new Error('Data is required');
    }
    if (typeof data !== 'object' || Array.isArray(data)) {
        throw new Error('Data must be an object');
    }
    if (typeof data.name !== 'string' || data.name.trim() === '') {
        throw new Error('Name must be a non-empty string');
    }
    if (typeof data.age !== 'number' || isNaN(data.age) || data.age < 0) {
        throw new Error('Age must be a positive number');
    }
    // Finally, your actual logic...
}

Or you could use @apexo/type and get it done in 3 lines:

import { isObject, isNotEmpty, isPositive } from '@apexo/type';

function processUser(data) {
    if (!isObject(data)) throw new Error('Data must be an object');
    if (!isNotEmpty(data.name)) throw new Error('Name is required');
    if (!isPositive(data.age)) throw new Error('Age must be positive');
    // Your actual logic, front and center
}

Why would I want that?

JavaScript's typeof operator falls short in many real-world scenarios:

  • typeof null === 'object' (it's not!)
  • typeof [] === 'object' (arrays need special handling)
  • No way to check for NaN, empty values, or validate complex structures

@apexo/type solves these problems by providing:

  • Runtime Safety: Validate data from APIs, user input, or any untrusted source before processing
  • TypeScript Integration: Type guards automatically narrow types in your code, giving you full IntelliSense support
  • Readable Code: Replace verbose checks like if (value !== null && value !== undefined && typeof value === 'string' && value.trim() !== '') with if (isNotEmpty(value))
  • Edge Case Handling: Correctly handles tricky JavaScript edge cases (null, undefined, NaN, Symbol, BigInt, circular references)

How little is it?

  • ~12KB minified (entire library)
  • Tree-shakeable: Only bundle what you use. Import just isString? Your bundle won't include date validators or circular reference detection
  • Zero dependencies: No bloat from external packages
  • Side-effect free: Marked as "sideEffects": false for optimal tree-shaking

How do I install it?

npm install @apexo/type

Or with your preferred package manager:

pnpm add @apexo/type
yarn add @apexo/type

How do I use it?

Basic Type Guards

Type guards return a boolean and automatically narrow TypeScript types:

import { isString, isNumber, isArray } from '@apexo/type';

function process(input: unknown) {
    if (isString(input)) {
        // TypeScript knows input is string here
        console.log(input.toUpperCase());
    }
    
    if (isArray(input)) {
        // TypeScript knows input is any[] here
        console.log(input.length);
    }
}

Validating Values

Beyond type checking, validate specific properties:

import { isPositive, isEven, isFutureDate, isEmpty } from '@apexo/type';

isPositive(10);  // true
isEven(7);       // false
isEmpty('   ');  // true
isFutureDate(new Date('2050-01-01')); // true

Advanced Utilities

import { whatTypeOf, hasCircularReference } from '@apexo/type';

whatTypeOf(null);        // "null" (not "object"!)
whatTypeOf([]);          // "array"
whatTypeOf(new Date());  // "date"

const obj: any = {};
obj.self = obj;
hasCircularReference(obj); // { hasCircular: true, path: "root.self -> root" }

Loading the library

import { isString, isObject } from '@apexo/type';

CommonJS

const { isString, isObject } = require('@apexo/type');

TypeScript

The library is written in TypeScript and includes full type definitions out of the box.

Are there TypeScript definitions?

Yes! The library is TypeScript-first and includes complete type definitions. All type guards use TypeScript's type predicate syntax (value is Type), providing automatic type narrowing:

function example(input: unknown) {
    if (isString(input)) {
        // input is automatically typed as 'string'
        input.toLowerCase(); // ✅ TypeScript knows this is safe
    }
}

Where can I use it?

Everywhere JavaScript runs:

  • ✅ Node.js (v14+)
  • ✅ Browsers (Modern browsers, IE11+ with polyfills)
  • ✅ Deno
  • ✅ Bun
  • ✅ Edge Functions (Vercel, Cloudflare Workers, etc.)
  • ✅ React Native

The library has zero dependencies and uses only standard JavaScript features, making it universally compatible.


API Reference

Primitives

  • isString(value) - Checks if value is a string
  • isNumber(value) - Checks if value is a number (and not NaN)
  • isNumberStr(value) - Checks if value is a number or numeric string
  • isBoolean(value) - Checks if value is a boolean
  • isNil(value) - Checks if value is null or undefined
  • isNull(value) - Checks if value is null
  • isUndefined(value) - Checks if value is undefined
  • isBigInt(value) - Checks if value is a BigInt
  • isSymbol(value) - Checks if value is a Symbol
  • isPrimitive(value) - Checks if value is a primitive type

Objects & Structures

  • isObject(value) - Checks if value is a non-null object (excluding arrays)
  • isPlainObject(value) - Checks if value is a plain object ({} or new Object())
  • isArray(value) - Checks if value is an array
  • isArrayLike(value) - Checks if value is array-like (has length property)
  • isFunction(value) - Checks if value is a function
  • isClass(value) - Checks if value is a class constructor
  • isIterable(value) - Checks if value is iterable
  • isAsyncIterable(value) - Checks if value is async iterable
  • isGeneratorFunction(value) - Checks if value is a generator function
  • isWindow(value) - Checks if value is a Window object

Advanced Types

  • isPromise(value) - Checks if value is a Promise
  • isMap(value) - Checks if value is a Map
  • isSet(value) - Checks if value is a Set
  • isWeakMap(value) - Checks if value is a WeakMap
  • isWeakSet(value) - Checks if value is a WeakSet
  • isRegExp(value) - Checks if value is a RegExp
  • isDate(value) - Checks if value is a Date object
  • isError(value) - Checks if value is an Error
  • isTypeError(value) - Checks if value is a TypeError
  • isReferenceError(value) - Checks if value is a ReferenceError
  • isSyntaxError(value) - Checks if value is a SyntaxError
  • isRangeError(value) - Checks if value is a RangeError
  • isURIError(value) - Checks if value is a URIError
  • isEvalError(value) - Checks if value is an EvalError
  • isAggregateError(value) - Checks if value is an AggregateError
  • isBuffer(value) - Checks if value is a Buffer (Node.js) or ArrayBuffer/View
  • isJson(value) - Checks if value is a valid JSON string

Empty Checks

  • isEmpty(value) - Checks if value is empty (null, undefined, empty string/array/object)
  • isNotEmpty(value) - Inverse of isEmpty
  • isEmptyStr(value) - Checks if value is an empty string (or whitespace only)
  • isEmptyArr(value) - Checks if value is an empty array
  • isEmptyObj(value) - Checks if value is an empty object

Number Validators

  • isFiniteNumber(value) - Checks if value is a finite number
  • isInteger(value) - Checks if value is an integer
  • isFloat(value) - Checks if value is a float
  • isSafeInteger(value) - Checks if value is a safe integer
  • isPositive(value) - Checks if value is > 0
  • isNegative(value) - Checks if value is < 0
  • isZero(value) - Checks if value is 0
  • isEven(value) - Checks if value is even
  • isOdd(value) - Checks if value is odd
  • isPrime(value) - Checks if value is a prime number
  • isInRange(value, min, max) - Checks if value is within [min, max]
  • isBetween(value, min, max) - Checks if value is strictly between (min, max)

Date Validators

  • isValidDate(value) - Checks if value is a valid Date (not Invalid Date)
  • isStrictDate(value) - Alias for isValidDate
  • isFutureDate(date) - Checks if date is in the future
  • isPastDate(date) - Checks if date is in the past
  • isToday(date) - Checks if date is today
  • isSameDay(a, b) - Checks if two dates are on the same day
  • isLeapYear(year) - Checks if year is a leap year
  • isISODateString(value) - Checks if value is an ISO 8601 date string
  • isValidISODateOnly(value) - Checks if value is a valid YYYY-MM-DD string
  • isValidTimeString(value) - Checks if value is a valid time string (HH:mm)
  • isDateTimeString(value) - Checks if value is a valid datetime string
  • isTimestamp(value) - Checks if value is a valid timestamp
  • isDateLike(value) - Checks if value can be converted to a valid Date
  • isForwardTime(date, offset?) - Checks if date is after now + offset
  • isBackwardTime(date, offset?) - Checks if date is before now - offset

Utilities

  • whatTypeOf(value) - Returns a specific type string (e.g., "array", "date", "null")
  • hasCircularReference(obj) - Detects circular references in objects/arrays

Architecture

The library is organized for maximum tree-shaking efficiency:

src/
├── guards/          # Type guards (value is Type)
│   ├── primitives.ts   # Basic types
│   ├── objects.ts      # Object structures
│   ├── advanced.ts     # Promises, Maps, Sets
│   ├── errors.ts       # Error types
│   └── empty.ts        # Empty checks
├── validators/      # Value validators (boolean)
│   ├── number.ts       # Numeric validations
│   └── date.ts         # Date validations
└── utils/           # Helper utilities
    ├── type-of.ts      # Enhanced typeof
    └── circular-ref.ts # Circular reference detection

This structure allows bundlers to eliminate entire categories of checks if you don't use them.


License

MIT

Repository

https://github.com/bytetologic/apexo.js/tree/main/packages/type

版本与发布记录

@apexo/type 共发布 3 个版本,首次发布于 2025/11/30;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.0.3latest2025/11/3011 个月前
0.0.12025/11/3011 个月前
0.0.22025/11/2611 个月前

关于 @apexo/type 的常见问题

@apexo/type 是什么?

@apexo/type is a small, zero-dependency library that gives you easy and reliable type checks in JavaScript and TypeScript.

如何安装 @apexo/type?

在项目目录运行 npm install @apexo/type,或使用 pnpm add / yarn add / bun add。

@apexo/type 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@apexo/type 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apexo/type 可以商用吗?

@apexo/type 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apexo/type 的下载量有多少?

根据 npm 官方下载统计,@apexo/type 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,772 次,日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apexo/type 还在维护吗?

最新版本 0.0.3 发布于 2025/11/30(11 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/11/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。