question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extended standard types and Symbol.toStringTag

See original GitHub issue

Might be a question more than a bug, but how would you expect this to react?

const type = require('type-detect');
class ExtendedArray extends Array {
  get [Symbol.toStringTag]() {
    return 'ExtendedArray';
  }
}
console.log(type(new ExtendedArray())); //=> 'array'

My initial expectation was that I would receive back ExtendedArray through the use of Symbol.toStringTag, but the code returns array, from here. Is this intended?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
dvlsgcommented, Jul 14, 2016

Hm. Here’s the official spec for “Array exotic objects”.

I poked around at a couple options, one of which was this, kept me at about 24,000,000 ops/sec (using Node v6.0.0 for this, by the way):

if (isArrayExists && Array.isArray(obj)) {
  if (symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined') {
    return 'array';
  }
  // return the toString.call(obj) + trims/lowercase from here as a shortcut?
}

This works too, but drops down to about 2,800,000 ops/sec for array literals on my machine:

if (isArrayExists && Array.isArray(obj)) {
  var arrayType = Object.prototype.toString.call(obj);
  if (arrayType === '[object Array]')
    return 'array';
}

I also tried dropping the Array.isArray() call entirely, and instead let it flow to the objPrototype section to see if objPrototype === Array.prototype. That got me about 18,000,000 ops/sec.

The first option above did allow me to drop through to the bottom toString.call() and receive back extendedarray. Here’s what I got with a few quick checks:

type(new ExtendedArray()); //=> 'extendedarray'
type([]); //=> 'array'
type(new Array()); //=> 'array'
type(new Uint8Array()); //=> 'uint8array'

Trying to think if that would be sufficient, or if I’m missing a scenario…

Another side note – is it intended that the custom toStringTag returns are lowercase?

2reactions
meebercommented, Jul 13, 2016

imo Symbo.toStringTag should take priority and return “ExtendedArray”, which would make this a bug.

@keithamus @lucasfcosta ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Symbol.toStringTag - JavaScript - MDN Web Docs
toStringTag well-known symbol is a string valued property that is used in the creation of the default string description of an object.
Read more >
String Literal types on es6 interfaces for [Symbol.toStringTag]?
I tried to create an interface that extends map and provides a different value for toStringTag but this doesn't work. interface MySpecialMap< ...
Read more >
Using JavaScript Symbol.toStringTag for objects types ...
toString() using Symbol.toStringTag , so to describe a type for our user object we can do the following: const user = { username:...
Read more >
Understanding Symbol: Javascript's Newest Primitive Type
First of all, symbol is a built-in primitive type. And it's guaranteed to be unique. Symbols are often used to add unique property...
Read more >
JS Symbol and Well-known symbols - Oleh Baranovskyi
The Symbol() function returns a value of type symbol, has static properties that expose several members of built-in objects, has static methods that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found