TypeScript: Can't use aliases of the global `Symbol` value to refer to its properties in the same manner or create unique symbols

Although Nodejs also uses this technique, we can’t do the same thing.

https://github.com/nodejs/node/search?q=primordials+symbol&unscoped_q=primordials+symbol

TypeScript Version: 3.7.x-dev.20200125

Search Terms:

Code

export { }
const Symbol = globalThis.Symbol;
const sym = Symbol();
[][Symbol.iterator];

Expected behavior:

const sym = Symbol(); // typeof sym
[][Symbol.iterator]; // Refer iterator method

Actual behavior:

const sym = Symbol(); // symbol
[][Symbol.iterator]; // error

Playground Link: http://www.typescriptlang.org/play/index.html?target=5&ts=3.8.0-dev.20200125&ssl=1&ssc=1&pln=2&pc=21#code/KYDwDg9gTgLgBAbzgXwFAGMIDsDO8DKAngLYBGEANnALxwDmFEpAhhQCoAWAljgHRFlKAbgzY8cHCRpwB5CgAoAlCIDaAXRWzKvLjGBRmMaGqA

Related Issues: #21603 #35478

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (12 by maintainers)

Most upvoted comments

@falsandtru if you can come cook dinner and tend to my kid I can work evenings

Can’t use aliases of the global Symbol value to refer to its properties in the same manner

And can’t make unique symbols using an alias of the Symbol function. Don’t miss it.

One unfortunate side-effect of the inability to alias the Symbol constructor when defining unique symbol property keys for classes and objects is that it results in increased bundle size, even when compiled with terser (see: https://github.com/terser/terser/issues/1337). Your compiled code becomes littered with Symbol() invocations.

Typically in libraries one will alias globals like Math to a local variable to work around terser failing to mangle built-ins, but given this TS limitation there is no way for one to do so.

The global Symbol properties will be fixed by #24738 #42543, which converts them all to unique symbol types.

Global variable reference greatly decreases the performance.

https://falsandtru.github.io/benchmark/suites/10/

But we can’t avoid this performance problem with Symbol caused by global reference because of this issue.