Can't evaluate equality of symbols made by Symbol.for
See original GitHub issueThis is also an obstacle of branded types and ghost types. I think resolving this problem is easy because these types are just a new literal type similar to string literal types.
TypeScript Version: 3.7.x-dev.20191228
Search Terms:
Code
const a = Symbol.for('');
const b = Symbol.for('');
const c: typeof b = Symbol.for(''); // error
a === b; // error
Expected behavior: no error Actual behavior: Type ‘typeof b’ is not assignable to type ‘typeof a’. This condition will always return ‘false’ since the types ‘typeof a’ and ‘typeof b’ have no overlap. Playground Link: https://www.typescriptlang.org/play/index.html?ts=3.8.0-dev.20191228&ssl=1&ssc=1&pln=4&pc=18#code/MYewdgzgLgBAhjAvDAygTwLYCMQBsB0AZiAE4AUA5BQJQDcAUKJLFkqpjgceVXY+NBjAAXDChoADgFMQhGK2TpseIqUo1aMAPRaYUkiVL0EiU-M069B0kA
Related Issues: #35200
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:10 (3 by maintainers)
Top Results From Across the Web
How to test equality of quoted symbols in Scheme?
This is good for instance on lists that have been read or calculated in various places and are the same, just not made...
Read more >Symbol type - The Modern JavaScript Tutorial
Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should...
Read more >Equals sign - Wikipedia
The equals sign (British English, Unicode) or equal sign (American English), also known as the equality sign, is the mathematical symbol =, which...
Read more >Math Symbols, Greater Than Or Equal To, Less ... - YouTube
In this video we discuss the equality and inequality math symbols. ... The equals symbol means that 2 values are equal.
Read more >Comparing Numbers — Symbols & Examples - Expii
Tip: The arrow-looking thing ("≤" or "≥") will always be pointing to the smaller number. "≥" means "greater than or equal to". For...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Throwing in a use case here: I’ve got a library that exports a symbol for purposes of interop, that other code can declare methods with that symbol, without needing to depend on the library that defines the symbol. But then, in a project that uses both of those libraries, Typescript can’t tell that module 2 implements the interface specified by module 1, even though it uses the same Symbol.for().
As it stands, the only way to get this kind of interop is with strings, not symbols, as there’s no “global symbol” or “symbol for” syntax in Typescript. Using
typeof
can’t work here because that would force the common dependency, and a shared type definitions file, since merely equivalent types are not sufficient for “unique symbol” to match.Like we have
unique symbol
, we should probably support:This would allow declaring Node’s built-in symbols as: