Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
falsandtru opened this issue Jan 28, 2020 · 15 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@falsandtru
Copy link
Contributor

falsandtru commented Jan 28, 2020

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

@falsandtru
Copy link
Contributor Author

falsandtru commented Jan 28, 2020

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.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Jan 29, 2020
@RyanCavanaugh
Copy link
Member

I don't quite understand what you're doing or why, or why what you're doing doesn't justify a type assertion

@falsandtru
Copy link
Contributor Author

what you're doing or why

To improve runtime performance as I explained above.

why what you're doing doesn't justify a type assertion

I'm not sure what you mean. What is meant by "justify a type assertion"?

@falsandtru
Copy link
Contributor Author

@RyanCavanaugh Can you triage the other issues too?

@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Jan 31, 2020
@RyanCavanaugh RyanCavanaugh changed the title Aliased Symbol doesn't work Can't use aliases of the global Symbol value to refer to its properties in the same manner Jan 31, 2020
@falsandtru
Copy link
Contributor Author

@RyanCavanaugh I'm not saying do it right now and the oldest issue in these days (#35896) was opened a month ago. Do you require me to wait more? 2 months?

@RyanCavanaugh
Copy link
Member

@falsandtru your issues are very confusing and require a lot of time to try to piece together what you mean and what you want to happen. You'll have to forgive me that I take up more actionable issues first; other people have put in the work to make understandable reports and are treated as such.

@falsandtru
Copy link
Contributor Author

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.

@RyanCavanaugh RyanCavanaugh changed the title Can't use aliases of the global Symbol value to refer to its properties in the same manner Can't use aliases of the global Symbol value to refer to its properties in the same manner or create unique symbols Jan 31, 2020
@falsandtru
Copy link
Contributor Author

your issues are very confusing

Can you point out where I don't know? It is difficult to know it myself.

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Mar 15, 2020

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

@bordoley
Copy link

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: terser/terser#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.

@RyanCavanaugh
Copy link
Member

The issue in the OP now behaves as desired, so I'm inclined to close this. @bordoley can you confirm or provide a failing repro?

@bordoley
Copy link

@RyanCavanaugh: I'll create a test repo, and file a new issue with an example that illustrates the issue.

@RyanCavanaugh
Copy link
Member

Thanks!

@bordoley
Copy link

see: #53282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants