or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-component-type

Type assertions aka less-broken `typeof`

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/component-type@2.0.x

To install, run

npx @tessl/cli install tessl/npm-component-type@2.0.0

index.mddocs/

Component Type

Component Type provides a more accurate alternative to JavaScript's native typeof operator for type checking and assertions. It correctly identifies various JavaScript types including primitives, objects, and special cases that the standard typeof operator handles inconsistently or incorrectly.

Package Information

  • Package Name: component-type
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install component-type

Core Imports

import type from "component-type";

For CommonJS:

const type = require("component-type");

Basic Usage

import type from "component-type";

// Basic type detection
console.log(type(new Date()));        // 'date'
console.log(type([]));               // 'array'
console.log(type(null));             // 'null'
console.log(type(NaN));              // 'nan'
console.log(type(/regex/));          // 'regexp'

// More accurate than native typeof
console.log(typeof null);            // 'object' (incorrect)
console.log(type(null));             // 'null' (correct)

console.log(typeof []);              // 'object' (less specific)
console.log(type([]));               // 'array' (more specific)

Capabilities

Type Detection Function

The main and only public API function that determines the type of any given value, providing more accurate results than JavaScript's native typeof operator.

/**
 * Determine the type of the given value
 * @param value - The value to determine the type of
 * @returns String representation of the value's type
 */
function type(value: unknown): string;

Supported Type Detections

The function can accurately detect and return string representations for the following types:

Primitives:

  • 'undefined' - for undefined values
  • 'null' - for null values
  • 'string' - for strings (including String wrapper objects)
  • 'number' - for numbers (including Number wrapper objects)
  • 'boolean' - for booleans (including Boolean wrapper objects)
  • 'function' - for functions

Special Numbers:

  • 'nan' - for NaN values (not detected as 'number')

Objects:

  • 'object' - for plain objects and other object types
  • 'array' - for arrays (including Array constructor instances)
  • 'date' - for Date objects
  • 'regexp' - for RegExp objects
  • 'error' - for Error objects
  • 'arguments' - for arguments objects

Environment-Specific:

  • 'element' - for HTML DOM elements (browser environments)
  • 'buffer' - for Node.js Buffer instances

Usage Examples

import type from "component-type";

// Primitive types
type('hello');                        // 'string'
type(42);                            // 'number'
type(true);                          // 'boolean'
type(undefined);                     // 'undefined'
type(null);                          // 'null'

// Function type
type(() => {});                      // 'function'

// Special number case
type(NaN);                           // 'nan'

// Object types
type({});                            // 'object'
type([1, 2, 3]);                     // 'array'
type(new Date());                    // 'date'
type(/pattern/);                     // 'regexp'
type(new Error('message'));          // 'error'

// Arguments object (inside a function)
function example() {
  return type(arguments);            // 'arguments'
}

// Environment-specific (browser)
if (typeof document !== 'undefined') {
  type(document.createElement('div')); // 'element'
}

// Environment-specific (Node.js)
if (typeof Buffer !== 'undefined') {
  type(Buffer.alloc(10));            // 'buffer'
}

// Wrapper objects are correctly identified
type(new String('hello'));           // 'string'
type(new Number(42));                // 'number'
type(new Boolean(true));             // 'boolean'

TypeScript Support

The package includes full TypeScript definitions with a single function declaration:

declare function type(value: unknown): string;
export = type;

Error Handling

The function is designed to handle any input value without throwing errors. It safely processes:

  • All JavaScript primitive types
  • All standard object types
  • Circular references (through safe property access)
  • Invalid or malformed objects
  • Cross-frame objects (in browser environments)

The function makes no guarantees about correctness when fed untrusted user input in security-critical contexts.