@se-oss/typeof
@se-oss/typeof is a robust utility for determining the runtime type of any JavaScript value.
Installation
npm install @se-oss/typeof
Install using your favorite package manager
pnpm
pnpm install @se-oss/typeof
yarn
yarn add @se-oss/typeof
Usage
Basic Usage
import { typeOf } from '@se-oss/typeof';
typeOf(undefined); // 'undefined'
typeOf(null); // 'null'
typeOf(true); // 'boolean'
typeOf('hello'); // 'string'
typeOf(123); // 'number'
typeOf([]); // 'array'
typeOf({}); // 'object'
typeOf(new Date()); // 'date'
typeOf(/abc/); // 'regexp'
TypeScript Support
The library provides a TypeOf<T> conditional type that maps TypeScript types to their runtime string representations.
import type { TypeOf } from '@se-oss/typeof';
type MyType = TypeOf<string>; // 'string'
type MyDate = TypeOf<Date>; // 'date'
The typeOf function also utilizes this type for better return type inference:
const result = typeOf('hello'); // Inferred as 'string'
Documentation
For all configuration options, please see the API docs.
Contributing
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated!
License
MIT Shahrad Elahi and contributors.