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

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

Open
4 tasks done
KSXGitHub opened this issue Oct 3, 2018 · 3 comments
Open
4 tasks done

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

KSXGitHub opened this issue Oct 3, 2018 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@KSXGitHub
Copy link
Contributor

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)
@KSXGitHub KSXGitHub changed the title Assign a known symbol to a constant Assign a known symbol to a constant should retain type information Oct 3, 2018
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 4, 2018
@DanielRosenwasser
Copy link
Member

@rbuckton any thoughts here?

@rbuckton
Copy link
Member

rbuckton commented Oct 4, 2018

The first example (const x = Symbol(); const y = x;) we initially disallowed because we were not able to write the declaration to an output declaration file (const x: unique symbol; const y: typeof x;). I will have to investigate to see if that has changed.

The second example (const { iterator } = Symbol;) won't be feasible until we are able to redeclare iterator (and the other symbols) as unique symbol.

@KSXGitHub
Copy link
Contributor Author

The second example (const { iterator } = Symbol;) won't be feasible until we are able to redeclare iterator (and the other symbols) as unique symbol.

I have realized this, so I made #27525

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Mar 13, 2019
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 3.4.0 milestone Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants