Type assertions aka less-broken `typeof`
npx @tessl/cli install tessl/npm-component-type@2.0.0Component 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.
npm install component-typeimport type from "component-type";For CommonJS:
const type = require("component-type");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)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;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 functionsSpecial 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 objectsEnvironment-Specific:
'element' - for HTML DOM elements (browser environments)'buffer' - for Node.js Buffer instancesimport 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'The package includes full TypeScript definitions with a single function declaration:
declare function type(value: unknown): string;
export = type;The function is designed to handle any input value without throwing errors. It safely processes:
The function makes no guarantees about correctness when fed untrusted user input in security-critical contexts.