Skip to content

Assign a known symbol to a constant should retain type information #27524

Open
@KSXGitHub

Description

@KSXGitHub

Search Terms

Suggestion

Assigning a known symbol constant to another constant should retain type information.

const foo = Symbol() // known symbol
const bar = foo // expecting 'typeof foo' but received 'symbol'

interface UseFoo {
  [foo]: 'value'
}

// This should work
const useBar: UseFoo = {
  [bar]: 'value'
}

Use Cases

Creating aliases of symbol constants without losing type information.

Examples

Symbol.iterator alias

const { iterator } = Symbol

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions