npm.io
1.1.0 • Published 9 months ago

@se-oss/typeof

Licence
MIT
Version
1.1.0
Deps
0
Size
17 kB
Vulns
0
Weekly
0
Stars
3

@se-oss/typeof

CI NPM Version MIT License npm bundle size Install Size

@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.

Keywords