Return precise type names for null, NaN, arrays, and more
The typeof operator has well-known gaps: typeof null is 'object', arrays are 'object', and NaN (literally “not a number”) is 'number'. Code that branches on types, such as loggers, serializers, and form parsers, usually needs something more precise.
Implement preciseTypeOf(value). It returns exactly one of these strings:
'null': null.'undefined': undefined.'boolean', 'string', 'bigint', 'symbol': primitives of that type.'nan': the number NaN.'number': any other primitive number, including Infinity, -Infinity, and -0.'array': any array, including arrays from another frame (<iframe>) or with a changed prototype, where instanceof Array is false.'function': anything callable, including arrow functions, async functions, generator functions, classes, and built-ins like Math.max.'object': everything else, including {}, Object.create(null), dates, maps, class instances, and boxed primitives like new String('a') or new Number(1).preciseTypeOf(null); // => 'null'
preciseTypeOf([4, 8]); // => 'array'
preciseTypeOf(0 / 0); // => 'nan'
preciseTypeOf(-Infinity); // => 'number'
preciseTypeOf(12n); // => 'bigint'
preciseTypeOf(class Player {}); // => 'function'
preciseTypeOf(new Boolean(false)); // => 'object'
preciseTypeOf(new Map()); // => 'object'
preciseTypeOf(Symbol('id')); // => 'symbol'