microsoft / microsoft/TypeScript
unique symbol lost on assignment to const despite type assertion
Nobody has claimed this yet.
- 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
💻 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:
- https://github.com/microsoft/TypeScript/issues/43657 - Widening on return type not on variable declaration
- https://github.com/microsoft/TypeScript/issues/55361 - Similar, explicitly typed variable is widened
- https://github.com/microsoft/TypeScript/issues/36876 - Widening when assigning from object property (but no assertion)
- https://github.com/microsoft/TypeScript/issues/53276
@Andarist perhaps https://github.com/microsoft/TypeScript/pull/54778 also fixes this?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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