{"id":21567518,"url":"https://github.com/alcidesqueiroz/whatype","last_synced_at":"2025-06-12T16:02:08.123Z","repository":{"id":55929517,"uuid":"126632795","full_name":"alcidesqueiroz/whatype","owner":"alcidesqueiroz","description":"🕵 No more headaches to find the type of a value in JavaScript. Whatype is a tiny module that saves you from some annoying type-related WTFJSes.","archived":false,"fork":false,"pushed_at":"2020-12-06T23:57:04.000Z","size":109,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T15:23:36.724Z","etag":null,"topics":["arrays","javascript","objects","primitives","types"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alcidesqueiroz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-24T19:21:50.000Z","updated_at":"2023-04-11T15:22:43.000Z","dependencies_parsed_at":"2022-08-15T09:40:41.049Z","dependency_job_id":null,"html_url":"https://github.com/alcidesqueiroz/whatype","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fwhatype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fwhatype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fwhatype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fwhatype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alcidesqueiroz","download_url":"https://codeload.github.com/alcidesqueiroz/whatype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244166637,"owners_count":20409177,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arrays","javascript","objects","primitives","types"],"created_at":"2024-11-24T10:31:26.217Z","updated_at":"2025-03-18T05:41:15.458Z","avatar_url":"https://github.com/alcidesqueiroz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# whatype [![Build status](https://travis-ci.com/alcidesqueiroz/whatype.svg?branch=master)](https://travis-ci.com/alcidesqueiroz/whatype)\n\n\u003e 🕵 No more headaches to find the type of a value in JavaScript. Whatype is a tiny module that saves you from some annoying type-related WTFJSes.\n\n\u003cimg src=\"https://gist.githubusercontent.com/alcidesqueiroz/c3d6c6edc559194bc37a2c464a21768d/raw/10b3f54010355c3bedfa3dbe7a1fb30ab0709c11/whatype.png\" width=\"300\" /\u003e\n\nWhat is a type? Well, this is surely one of the most debated topics in computer science. This four-letter word means one thing in static type systems and something else, completely different, in dynamic type systems. **I won't dwell on that.** This module focus on the **practical/mundane/non-academic meaning of \"type\"**, with what we deal with everyday at work, more specifically in JavaScript (which isn't an example when it comes to type systems). =)\n\n## Install\n\nWith npm:\n```\n$ npm install whatype\n```\n\nWith Yarn:\n\n```\n$ yarn add whatype\n```\n\n## Usage\nWhatype has two possible usages:\n1. By calling the `whatype` function itself, which returns a string with the detected type.\n2. By calling the `whatype.is` method, which checks if a value is of a specific type. For some needs, this form is way more versatile, since it allows checking for things like \"pure-object\", \"literal-object\",  \"empty-array\", \"empty-object\", \"falsy\", \"truthy\" and other possibilities.\n\n### whatype(value)\n\nReturns a string with the type of the passed value.\n\n```javascript\nconst whatype = require('whatype');\n\n// String primitives\nwhatype('whatever'); // =\u003e 'string'\n\n// String objects are different from string primitives. Their typeof is \"object\".\n// Whatype will normalize this, treating them in the same way as primitives\n// If you want to differentiate between string objects and primitives, use whatype.is method instead\nwhatype(new String('foo')); // =\u003e 'string'\n\n// No more typeof null === \"object\"... =)\nwhatype(null); // =\u003e 'null'\n\nwhatype(undefined); // =\u003e 'undefined'\n\n// No more typeof NaN === \"number\"... =)\n// Differently from what IEEE 754-1985 (and its 2008 revision) specifies,\n// Whatype will not consider NaN as a number\nwhatype(NaN); // =\u003e 'not-a-number'\n\n// Number primitives\nwhatype(0); // =\u003e 'number'\nwhatype(42); // =\u003e 'number'\nwhatype(30.5); // =\u003e 'number'\nwhatype(-17); // =\u003e 'number'\n\n// Number objects are different from number primitives. Their typeof is \"object\".\n// Whatype will normalize this, treating them in the same way as primitives\n// If you want to differentiate between number objects and primitives, use whatype.is method instead\nwhatype(new Number('12')); // =\u003e 'number'\n\n// Boolean primitives\nwhatype(true); // =\u003e 'boolean'\nwhatype(false); // =\u003e 'boolean'\n\n// Boolean objects are different from boolean primitives. Their typeof is \"object\".\n// Whatype will normalize this, treating them in the same way as primitives\n// If you want to differentiate between boolean objects and primitives, use whatype.is method\nwhatype(new Boolean(1)); // =\u003e 'boolean'\n\n// Differently from what IEEE 754-1985 (and its 2008 revision) specifies,\n// Whatype will not consider Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY as numbers\nwhatype(Infinity); // =\u003e 'infinity'\nwhatype(-Infinity); // =\u003e '-infinity'\n\nwhatype(function() { return arguments; }()); // =\u003e 'arguments'\nwhatype({}); // =\u003e 'object'\nwhatype(Object.create(null)); // =\u003e 'object'\nwhatype(new function(){}); // =\u003e 'object'\nwhatype({ a: 123 }); // =\u003e 'object'\nwhatype(/\\d/); // =\u003e 'regexp'\n\n// No more typeof [] === \"object\"... =)\n// P.S: I know that arrays ARE objects. But in 99% of cases, you'll want to differentiate them\nwhatype([]); // =\u003e 'array'\nwhatype([ 11, 22 ]); // =\u003e 'array'\n\nwhatype(new Promise(() =\u003e {})); // =\u003e 'promise'\nwhatype(function(){}); // =\u003e 'function'\nwhatype(async function (){}); // =\u003e 'async-function'\nwhatype(function *(){}); // =\u003e 'generator-function'\nwhatype(() =\u003e {}); // =\u003e 'function'\nwhatype(Symbol('foo')); // =\u003e 'symbol'\nwhatype(new Date()); // =\u003e 'date'\n\n// The returned type for every kind of error is simply \"error\", not \"object\"\n// If you want to test for a specific type of error, use the whatype.is method instead\nwhatype(new Error()); // =\u003e 'error'\nwhatype(new ReferenceError()); // =\u003e 'error'\nwhatype(new EvalError()); // =\u003e 'error'\nwhatype(new TypeError()); // =\u003e 'error'\nwhatype(new URIError()); // =\u003e 'error'\nwhatype(new RangeError()); // =\u003e 'error'\nwhatype(new SyntaxError()); // =\u003e 'error'\n\n// Maps\nwhatype(new Map()); // =\u003e 'map'\nwhatype(new WeakMap()); // =\u003e 'weak-map'\n\n// Sets\nwhatype(new Set()); // =\u003e 'set'\nwhatype(new WeakSet()); // =\u003e 'weak-set'\n\n// The returned type for every kind of typed array is simply \"typed-array\", not \"object\"\n// If you want to test for a specific type of typed array, use the whatype.is method instead\nwhatype(new Float32Array()); // =\u003e 'typed-array'\nwhatype(new Float64Array()); // =\u003e 'typed-array'\nwhatype(new Int16Array()); // =\u003e 'typed-array'\nwhatype(new Int32Array()); // =\u003e 'typed-array'\nwhatype(new Int8Array()); // =\u003e 'typed-array'\nwhatype(new Uint16Array()); // =\u003e 'typed-array'\nwhatype(new Uint32Array()); // =\u003e 'typed-array'\nwhatype(new Uint8Array()); // =\u003e 'typed-array'\nwhatype(new ArrayBuffer(10)); // =\u003e 'array-buffer'\n\nwhatype(new DataView(new ArrayBuffer())); // =\u003e 'data-view'\n```\n\n### whatype.is(value, type)\n\nReturns a boolean indicating if the value is of the supplied type.\n\n```javascript\nconst whatype = require('whatype');\n\n// Primitives\nwhatype.is('foo', 'string-primitive'); //=\u003e true\nwhatype.is(new String('foo'), 'string-primitive'); //=\u003e false\nwhatype.is(42, 'number-primitive'); //=\u003e true\nwhatype.is(new Number('12'), 'number-primitive'); //=\u003e false\nwhatype.is(false, 'boolean-primitive'); //=\u003e true\nwhatype.is(new Boolean(1), 'boolean-primitive'); //=\u003e false\n\n// String, Number and Boolean objects\nwhatype.is(new String('foo'), 'string-object'); //=\u003e true\nwhatype.is('foo', 'string-object'); //=\u003e false\nwhatype.is(new Number('12'), 'number-object'); //=\u003e true\nwhatype.is(12, 'number-object'); //=\u003e false\nwhatype.is(new Boolean(1), 'boolean-object'); //=\u003e true\nwhatype.is(true, 'boolean-object'); //=\u003e false\n\n// Falsy values\nwhatype.is('', 'falsy'); //=\u003e true\nwhatype.is(new String(''), 'falsy'); //=\u003e true\nwhatype.is(undefined, 'falsy'); //=\u003e true\nwhatype.is(null, 'falsy'); //=\u003e true\nwhatype.is(undefined, 'falsy'); //=\u003e true\nwhatype.is(NaN, 'falsy'); //=\u003e true\nwhatype.is(0, 'falsy'); //=\u003e true\nwhatype.is(false, 'falsy'); //=\u003e true\nwhatype.is(new Boolean(0), 'falsy'); //=\u003e true\n\n// Truthy values\nwhatype.is(42, 'truthy'); //=\u003e true\nwhatype.is('whatever', 'truthy'); //=\u003e true\nwhatype.is({}, 'truthy'); //=\u003e true\nwhatype.is([], 'truthy'); //=\u003e true\nwhatype.is(true, 'truthy'); //=\u003e true\nwhatype.is(new Boolean(1), 'truthy'); //=\u003e true\n\n// Strings in general\nwhatype.is('whatever', 'string'); //=\u003e true\nwhatype.is(new String('foo'), 'string'); //=\u003e true\n\nwhatype.is(null, 'null'); //=\u003e true\nwhatype.is(undefined, 'undefined'); //=\u003e true\n\n// 'nil' stands for null or undefined\nwhatype.is(null, 'nil'); //=\u003e true\nwhatype.is(undefined, 'nil'); //=\u003e true\n\nwhatype.is(NaN, 'not-a-number'); //=\u003e true\n\n// Differently from what IEEE 754-1985 (and its 2008 revision) specifies, Whatype will not consider\n// NaN as a number, but it IS considered a SORT OF numeric value.\nwhatype.is(NaN, 'number'); //=\u003e false\nwhatype.is(NaN, 'numeric'); //=\u003e true\n\nwhatype.is(Infinity, 'infinity'); //=\u003e true\nwhatype.is(-Infinity, '-infinity'); //=\u003e true\n\n// Differently from what IEEE 754-1985 (and its 2008 revision) specifies, Whatype will not consider\n// Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY as numbers, but these values are considered\n// a sort of numeric value.\nwhatype.is(Infinity, 'number'); //=\u003e false\nwhatype.is(-Infinity, 'number'); //=\u003e false\nwhatype.is(Infinity, 'numeric'); //=\u003e true\nwhatype.is(-Infinity, 'numeric'); //=\u003e true\n\n// Numbers in general\nwhatype.is(42, 'number'); //=\u003e true\nwhatype.is(42, 'numeric'); //=\u003e true\nwhatype.is(new Number('12'), 'number'); //=\u003e true\nwhatype.is(new Number('12'), 'numeric'); //=\u003e true\n\n// Booleans in general\nwhatype.is(true, 'boolean'); //=\u003e true\nwhatype.is(false, 'boolean'); //=\u003e true\nwhatype.is(new Boolean(1), 'boolean'); //=\u003e true\n\n// Arguments objects\nwhatype.is(function() { return arguments; }(), 'arguments'); //=\u003e true\nwhatype.is(function() { return arguments; }(), 'object'); //=\u003e true\n\nwhatype.is({}, 'object'); //=\u003e true\nwhatype.is(new function A(){}, 'object'); //=\u003e true\n\n// A literal object are those whose constructor is Object\nwhatype.is({ a: 123 }, 'literal-object'); //=\u003e true\nwhatype.is(new function A(){}, 'literal-object'); //=\u003e false\n\n// Any literal object without own properties\nwhatype.is({}, 'empty-literal-object'); //=\u003e true\nwhatype.is(new function A(){}, 'empty-literal-object'); //=\u003e false\n\nwhatype.is(Object.create(null), 'object'); //=\u003e true\n\n// A pure object is an object created with Object.create(null).\n// It neither has a constructor, nor inherits methods from the Object prototype.\nwhatype.is(Object.create(null), 'pure-object'); //=\u003e true\nwhatype.is({}, 'pure-object'); //=\u003e false\n\nwhatype.is(/\\d/, 'regexp'); //=\u003e true\n\n// Arrays\nwhatype.is([], 'array'); //=\u003e true\nwhatype.is([], 'empty-array'); //=\u003e true\nwhatype.is([ 11, 22 ], 'empty-array'); //=\u003e false\n\n// Functions\nwhatype.is(function(){}, 'function'); //=\u003e true\nwhatype.is(() =\u003e {}, 'function'); //=\u003e true\nwhatype.is(async function (){}, 'function'); //=\u003e true\nwhatype.is(function *(){}, 'function'); //=\u003e true\n\n// Specific types of function\nwhatype.is(function(){}, 'common-function'); //=\u003e true\nwhatype.is(async function (){}, 'async-function'); //=\u003e true\nwhatype.is(function *(){}, 'generator-function'); //=\u003e true\n\nwhatype.is(Symbol('foo'), 'symbol'); //=\u003e true\nwhatype.is(new Promise(() =\u003e {}), 'promise'); //=\u003e true\nwhatype.is(new Promise(() =\u003e {}), 'object'); //=\u003e true\nwhatype.is(new Date(), 'date'); //=\u003e true\nwhatype.is(new Date(), 'object'); //=\u003e true\nwhatype.is(new Error(), 'error'); //=\u003e true\nwhatype.is(new Error(), 'object'); //=\u003e true\n\n// Errors\nwhatype.is(new ReferenceError(), 'error'); //=\u003e true\nwhatype.is(new EvalError(), 'error'); //=\u003e true\nwhatype.is(new TypeError(), 'error'); //=\u003e true\nwhatype.is(new URIError(), 'error'); //=\u003e true\nwhatype.is(new RangeError(), 'error'); //=\u003e true\nwhatype.is(new SyntaxError(), 'error'); //=\u003e true\n\n// Specific types of error\nwhatype.is(new ReferenceError(), 'reference-error'); //=\u003e true\nwhatype.is(new EvalError(), 'eval-error'); //=\u003e true\nwhatype.is(new TypeError(), 'type-error'); //=\u003e true\nwhatype.is(new URIError(), 'uri-error'); //=\u003e true\nwhatype.is(new RangeError(), 'range-error'); //=\u003e true\nwhatype.is(new SyntaxError(), 'syntax-error'); //=\u003e true\n\n// Maps\nwhatype.is(new Map(), 'map'); //=\u003e true\nwhatype.is(new WeakMap(), 'weak-map'); //=\u003e true\n\n// Sets\nwhatype.is(new Set(), 'set'); //=\u003e true\nwhatype.is(new WeakSet(), 'weak-set'); //=\u003e true\n\n// Typed arrays\nwhatype.is(new Float32Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Float64Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Int16Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Int32Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Int8Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Uint16Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Uint32Array(), 'typed-array'); //=\u003e true\nwhatype.is(new Uint8Array(), 'typed-array'); //=\u003e true\n\n// Specific types of typed arrays\nwhatype.is(new Float32Array(), 'float32-array'); //=\u003e true\nwhatype.is(new Float64Array(), 'float64-array'); //=\u003e true\nwhatype.is(new Int16Array(), 'int16-array'); //=\u003e true\nwhatype.is(new Int32Array(), 'int32-array'); //=\u003e true\nwhatype.is(new Int8Array(), 'int8-array'); //=\u003e true\nwhatype.is(new Uint16Array(), 'uint16-array'); //=\u003e true\nwhatype.is(new Uint32Array(), 'uint32-array'); //=\u003e true\nwhatype.is(new Uint8Array(), 'uint8-array'); //=\u003e true\nwhatype.is(new ArrayBuffer(10), 'array-buffer'); //=\u003e true\n\nwhatype.is(new DataView(new ArrayBuffer()), 'data-view'); //=\u003e true\n```\n\n## Author\n\nAlcides Queiroz Aguiar\n\n- Website: [www.alcidesqueiroz.com](https://www.alcidesqueiroz.com)\n- Medium: [@alcidesqueiroz](https://medium.com/@alcidesqueiroz)\n- Twitter: [alcidesqueiroz](https://twitter.com/alcidesqueiroz)\n- Behance [alcidesqueiroz](https://behance.net/alcidesqueiroz)\n- Stack Overflow: [http://is.gd/aqanso](http://stackoverflow.com/users/1295666/alcides-queiroz-aguiar)\n- E-mail: alcidesqueiroz \u0026lt;at\u0026gt; gmail\n\n## License\n\nThis code is free to use under the terms of the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcidesqueiroz%2Fwhatype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falcidesqueiroz%2Fwhatype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcidesqueiroz%2Fwhatype/lists"}