Skip to content

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

Closed
@KSXGitHub

Description

@KSXGitHub

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptCommittedThe team has roadmapped this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions