Why ES6 introduced classes yet `Symbol` not to be used with `new`?

Alexander Jones alex at weej.com
Mon Aug 15 23:03:48 UTC 2016

On 15 August 2016 at 19:20, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:

> Because Symbol does not have a syntactic literal form, some sort of
> invocation is required to acquire a new unique primitive Symbol value.
> That is where you get the potential for confusion between `Symbol()` and
> `new Symbol()`.  And minimizing this sort of mistake is not just a matter
> of needing “better learning”.  Even an experts can have momentary mental
> slips and type a `new Symbol()` when they really meant `Symbol()`.

Honestly, I think this kind of basic type error is something which is so
trivially catchable at a very early stage when you have even a semblance of
static analysis. `new Symbol()` should give you an object of class Symbol,
and `Symbol()` should give you a symbol - and TypeScript even without
annotations is going to give you a pretty strong warning. IMO it's a bit
ugly to break the pattern of primitive wrappers being newable for the sake
of saving programming errors, when the analyzer can do a much more complete
job anyway.

let s1 = String("banana"); // string
let s2 = new String("banana"); // object
let o = {banana: 2};
o[s2]; // red squiggly: An index expression argument must be of type
'string', 'number', 'symbol', or 'any'.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160816/18045283/attachment.html>

More information about the es-discuss mailing list