microsoft / microsoft/TypeScript

unique symbol lost on assignment to const despite type assertion

Open
#55,901 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

unique symbol widening

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground Link

💻 Code
const u = Symbol();
const a7 = u as typeof u // widened  to symbol
🙁 Actual behavior

a7 is symbol

🙂 Expected behavior

a7 has typeof u

Additional information about the issue

This issue is a huge problem for isolated declarations as it breaks the assumption that in an expression const o = a as T we can always use T as the type of o. This is also inconsistent with the way similar expressions work for string/number literal types (ex)

Similar issues:

@Andarist perhaps https://github.com/microsoft/TypeScript/pull/54778 also fixes this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and compare the inferred types for the unique-symbol assertion with the analogous string and number literal cases. Trace the compiler's widening behavior for const declarations and type assertions, then verify that the asserted unique-symbol type is preserved without regressing the related cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.