question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't evaluate equality of symbols made by Symbol.for

See original GitHub issue

This 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:open
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
pjebycommented, Jun 19, 2022

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.

2reactions
ExE-Bosscommented, Feb 14, 2020

Like we have unique symbol, we should probably support:

const foo: global symbol "<name>";

This would allow declaring Node’s built-in symbols as:

declare module "util" {
	namespace inspect {
		// https://github.com/nodejs/node/issues/20821
		const custom: global symbol "nodejs.util.inspect.custom";
	}

	namespace promisify {
		// https://github.com/nodejs/node/issues/31647
		const custom: global symbol "nodejs.util.promisify.custom";
	}
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found