fork download
  1. Symbol.type = Symbol('type')
  2. const NaO = Symbol.for('NaO')
  3.  
  4. Reflect.type = object => object != null ? object[Symbol.type] : NaO // Not a Object ;-)
  5.  
  6. 'Number String Array Object Boolean Symbol' // add more
  7. .split(' ')
  8. .forEach(b => eval(b).prototype[Symbol.type] = b)
  9.  
  10. console.log([
  11. 1, 1.1, 'str', {}, true, [], null, undefined, new String('something'), new Array(5), {[Symbol.type]: 'CustomType'}, NaN, NaO
  12. ].map(v => [(typeof v), Reflect.type(v)]))
Success #stdin #stdout 0.04s 146816KB
stdin
Standard input is empty
stdout
[ [ 'number', 'Number' ],
  [ 'number', 'Number' ],
  [ 'string', 'String' ],
  [ 'object', 'Object' ],
  [ 'boolean', 'Boolean' ],
  [ 'object', 'Array' ],
  [ 'object', Symbol(NaO) ],
  [ 'undefined', Symbol(NaO) ],
  [ 'object', 'String' ],
  [ 'object', 'Array' ],
  [ 'object', 'CustomType' ],
  [ 'number', 'Number' ],
  [ 'symbol', 'Symbol' ] ]