Skip to content

Symbol description property should not forget its value. #56535

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

Closed
rotu opened this issue Nov 25, 2023 · 5 comments
Closed

Symbol description property should not forget its value. #56535

rotu opened this issue Nov 25, 2023 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@rotu
Copy link

rotu commented Nov 25, 2023

🔎 Search Terms

symbol, description

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?esModuleInterop=false&declaration=false&target=9&jsx=0&module=7&stripInternal=false&noErrorTruncation=true&ts=5.4.0-dev.20231124&ssl=10&ssc=48&pln=1&pc=1#code/MYewdgzgLgBBCeBbGBeGBlJAjEAbAFAOQBmIIhAlAFBUD0tMUAFgJYQwCGEELA5mIgCmYWBCYgArrgAmMAO4gATgGsqoSLAAeALgDe0wRGCKWABygtw2kmUIBfVHCQ16jVuwUqOiyWFnSQQxgwEFhPVXVoGHhHBGQuGHwJMBYARwlBJ0QcXBgAMhh9Q2MzCysbcjtqOgZmNnklZXYsCVhBRS4guJyYZLSMsEMINXAogC9YpE52fG68fMKDIxNzSzBrUkqKIA

💻 Code

const sym = Symbol('foo')

// this assignment should work
const x:{description:'foo'} = sym

// this workaround does not work
const y = sym as (unique symbol & {description:'foo'})

// this works but erases symbol uniqueness
const z = sym as (symbol & {description:'foo'})

🙁 Actual behavior

The assignment fails with

Type 'typeof sym' is not assignable to type '{ description: "foo"; }'.

Typescript infers sym as a unique symbol with a readonly description property of type string | undefined.

Note that you can't correct this with an intersection type (as also illustrated in the example)

🙂 Expected behavior

Typescript should infer that description is of type "foo" and permit the assignment.

Additional information about the issue

No response

@andrewbranch andrewbranch added the Question An issue which isn't directly actionable in code label Nov 27, 2023
@andrewbranch
Copy link
Member

You’re looking for

- const y = sym as (unique symbol & {description:'foo'})
+ const y = sym as (typeof sym & {description:'foo'})

@rotu
Copy link
Author

rotu commented Nov 28, 2023

@andrewbranch You're right! Adding a wrapper type works.

It seems an assertion does as well:

const sym = Symbol('foo')
assert(sym.description === 'foo')

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2023
@rotu
Copy link
Author

rotu commented Dec 1, 2023

@andrewbranch why did you mark this as a question? Is there a design reason the auxiliary type or the assertion should be needed at all?

@andrewbranch
Copy link
Member

Yes, this is working as intended; I sometimes use "Question" to indicate that the OP was trying to solve a problem and found a solution, even if they went in thinking they were seeing a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants