facebook / facebook/flow

get [Symbol.toStringTag]() : string and Object.prototype.toString.apply

Open
#3,212 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

So somewhere between computed property keys and changing the type of an object, the Symbol.toStringTag changes the output of an object that uses this defined key. By default Object.prototype.toString.apply can be used on any object to find an underlying type. Used on a regular expression it returns [object RegExp] or on a string it returns [object String].

On an object literal or even a class instance, it returns [object Object]. So the ES consortium came up with Symbol.toStringTag. When using this on an object, either via Object.defineProperty() or as a class getter with `get [Symbol.toStringTag]()`, you can coerce the type of the object to be something else.

```javascript
class Person {
get [Symbol.toStringTag]() {
return 'Person';
}
}

let person : Person = new Person();
let toString : Function = Object.prototype.toString;
console.log(toString.apply(person)); // '[object Person]'

let someObj : object = {};
console.log(toString.apply(someObj)); // '[object Object]'
Object.defineProperty(someObj, Symbol.toStringTag, {
get: function() : string {
return 'Person';
}
});
console.log(toString.apply(someObj)); // '[object Person]'
```

My questions are around how flowtype should and will evaluate objects or class instances that have a specified Symbol.toStringTag. IMHO it should report them as a class instance of the value returned by the getter.

Currently, none of this works with flowtype.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.