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
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 15 (12 by maintainers)
@falsandtru if you can come cook dinner and tend to my kid I can work evenings
And can’t make unique symbols using an alias of the
Symbolfunction. 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
Symbolproperties will be fixed by#24738#42543, which converts them all tounique symboltypes.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.