@se-oss/typeof is a robust utility for determining the runtime type of any JavaScript value.
npm install @se-oss/typeofInstall using your favorite package manager
pnpm
pnpm install @se-oss/typeofyarn
yarn add @se-oss/typeofimport { 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'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'For all configuration options, please see the API docs.
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! 🙏
MIT © Shahrad Elahi and contributors.