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

Make Symbol.* (e.g. Symbol.iterator) unique symbols #27525

Closed
4 tasks done
KSXGitHub opened this issue Oct 3, 2018 · 4 comments · Fixed by #42543
Closed
4 tasks done

Make Symbol.* (e.g. Symbol.iterator) unique symbols #27525

KSXGitHub opened this issue Oct 3, 2018 · 4 comments · Fixed by #42543
Labels
Bug A bug in TypeScript Committed The team has roadmapped this issue
Milestone

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Oct 3, 2018

Search Terms

Suggestion

Use this:

declare function Symbol (x?: string | number): symbol

declare namespace Symbol {
  const prototype: symbol
  const iterator: unique symbol
}

Or this:

interface SymbolConstructor {
  (x?: string | number): symbol
  readonly prototype: symbol
  readonly iterator: unique symbol
}

declare var Symbol: SymbolConstructor

Instead of this:

interface SymbolConstructor {
  (x?: string | number): symbol
  readonly prototype: symbol
  readonly iterator: symbol
}

declare var Symbol: SymbolConstructor

Use Cases

Right now, I can do this for custom unique symbol:

const foo = Symbol() // unique symbol
const bar: typeof foo = foo

const baz: {
	[foo]: number
} = {
	[bar]: 123
}

But not for Symbol.*

// Expecting 'typeof Symbol.iterator' but received 'symbol'
const iterator: typeof Symbol.iterator = Symbol.iterator

// This is not working
const iterable: Iterable<number> = {
  * [iterator] () {
    yield * [0, 1, 2, 3]
  }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

Playground Link (This also works)

Related Issue

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Oct 3, 2018
@RyanCavanaugh
Copy link
Member

@rbuckton what are your thoughts on this?

@rbuckton
Copy link
Member

rbuckton commented Oct 3, 2018

This is something we plan to do, but requires us to support "typesVersions" on DefinitelyTyped so that we can address the forward-declaration of Symbol.iterator, etc. in the type definitions for NodeJS, core-js, and es6-shim.

@weswigham
Copy link
Member

#24738 also works around the old-lib issue by considering any symbols under the global Symbol constructor as unique symbols.

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Jan 3, 2020

#24738 would fix this in a manner that’s compatible with old .d.ts files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Committed The team has roadmapped this issue
Projects
None yet
6 participants