Skip to content

Symbol = LiveSymbol | DeadSymbol #59574

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

Open
6 tasks done
conartist6 opened this issue Aug 9, 2024 · 5 comments
Open
6 tasks done

Symbol = LiveSymbol | DeadSymbol #59574

conartist6 opened this issue Aug 9, 2024 · 5 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

@conartist6
Copy link

conartist6 commented Aug 9, 2024

πŸ” Search Terms

symbol weakmap TC39

βœ… Viability Checklist

⭐ Suggestion

The latest version of ES creates a new kind of runtime error. It allows symbols to be used as weak map keys, but only some symbols. In particular it splits the symbol primitive in two. One one hand are "live" symbols, tracked by the garbage collector, and usable as weak map keys. Symbol() creates a live symbol. On the other had are "dead" symbols, such as Symbol.iterator, which return some value from Symbol.keyFor(sym) and are not legal keys for weak maps.

Since there are now fundamentally two incompatible primitive types, TS should adjust so that it is capable of catching these errors again.

πŸ“ƒ Motivating Example

const protos = new WeakMap();

const sym = Math.random() > 0.5 ? Symbol() : Symbol.for('me');

protos.set(sym, null); // crashes half the time

πŸ’» Use Cases

Catching more errors

@conartist6
Copy link
Author

There is a related request for clarification from TC39 here: https://es.discourse.group/t/symbol-registration-and-weakmap-keying/2101/7

@whzx5byb
Copy link

whzx5byb commented Aug 9, 2024

Seems like a intended behavior, per #52534 (comment).

@conartist6
Copy link
Author

Even if the maintainers have considered this before I would like for it to have the weight of a formal feature request, so that demand for the feature may be tracked over the long term.

I presume that right now demand for the feature is basically zero since few to no people will be using symbols as weakmap keys in the wild yet. Once there is real damage to productivity from these kinds of errors I expect demand for the feature to begin to grow, and that demand can be tracked here.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Aug 9, 2024
@RyanCavanaugh
Copy link
Member

Useful information would be how someone might accidently make this kind of error (apart from the very first time, which would statically fail)

@Josh-Cena
Copy link
Contributor

Friendly reminder that Symbol.iterator is eligible as weakmap keys, because Symbol.iterator has similar garbage collection semantics as Array.prototype. Therefore in MDN (and also spec discussions, I think?) we use "non-registered symbol" for symbols that can be weakly held.

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