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

Symbols in const assertions should be considered unique #35562

Open
loilo opened this issue Dec 7, 2019 · 5 comments
Open

Symbols in const assertions should be considered unique #35562

loilo opened this issue Dec 7, 2019 · 5 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

@loilo
Copy link

loilo commented Dec 7, 2019

TypeScript Version: 3.8.0-dev.20191205

Search Terms:

  • const assertion unique symbol
  • "unique symbol" "as const"

Code

const prop = Symbol()
const dictionary = {
  prop: Symbol()
}
const sealedDictionary = {
  prop: Symbol()
} as const

class MyClass {
  // Works because "prop" is a unique symbol
  [prop]: 1

  // Does not work because "dictionary.prop" is not a unique symbol
  [dictionary.prop]: 2

  // Does not work because "sealedDictionary.prop" is not a unique symbol,
  // even though it probably should be?
  [sealedDictionary.prop]: 3
}

Expected behavior:

MyClass should probably accept sealedDictionary.prop as a computed property name since it should be considered a unique symbol.

Actual behavior:

TypeScript complains with TS1166:

A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.

Playground Link: Provided Updated (old link seems to be broken in new playground)

Related Issues:

@RyanCavanaugh RyanCavanaugh added 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 labels Jan 24, 2020
@LANGERGabrielle
Copy link

It seems that symbols lose their "unique" when assigned to an object.

Another example of this behaviour: Playground.

@kyeotic
Copy link

kyeotic commented Feb 17, 2021

@RyanCavanaugh you marked this as "awaiting more feedback". Can you elaborate on what that means? Is it community feedback or maintainer feedback you are looking for?

I ran into this problem the other and was convinced for a while that it was an issue with typescripts symbol index support, until I was pointed here. It would be really nice if this was fixed.

@jamesopstad
Copy link

jamesopstad commented May 7, 2022

Is the current behaviour intentional or an oversight? It's frustrating that to create an object of unique symbols you have to do this:

const a = Symbol();
const b = Symbol();
const symbols = { a, b } as const;

It would be nicer if you could declare the symbols directly:

const symbols = {
  a: Symbol(),
  b: Symbol()
} as const;

@omerd-cyera
Copy link

Any updates?
I ran into the same weird behaviour. Also, for my case i dont want the type to have readonly, so im looking for another way of creating the object.

@NWYLZW
Copy link

NWYLZW commented Dec 14, 2023

It is very inconvenient to write such code now. I have to do this:

const fooSymbols = {
  a: Symbol(),
  b: Symbol()
} as {
  readonly a: unique symbol
  readonly b: unique symbol
}

But there are indeed some things to consider about this issue, such as [[Getter]]:

const _b = Symbol()
const fooSymbols = {
  get a() {
    return Symbol()
  },
  get b() {
    return _b
  }
} as const

Is it possible to not process the properties corresponding to [[Getter]] but to process the properties of expression?
Processing refers to converting the corresponding symbol into unique symbol by as const.

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

7 participants