microsoft / microsoft/TypeScript

Explicitly annotated unique symbols are widened in return positions

Open
#55,361 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Literal Types Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

widening return unique symbol

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

https://www.typescriptlang.org/play?ts=5.3.0-dev.20230814&ssl=6&ssc=2&pln=1&pc=1#code/KYDwDg9gTgLgBAYwgOwM7xsdcC8cDKAngLYBGEANgBQCUAUHaJLHAGYCuyCMAlim8lpwA3nTjjEKbAEMKPaagBccGITDAIrFVnh5M6ANxiJUYDHZRkcWfNR0AvkA

💻 Code
export const test = Symbol()

export function fn() {
    const alias: typeof test = test;
    return alias
}
🙁 Actual behavior

this declaration file is emitted:

export declare const test: unique symbol;
export declare function fn(): symbol;
🙂 Expected behavior

this declaration file to be emitted:

export declare const test: unique symbol;
export declare function fn(): typeof test;
Additional information

getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded calls getWidenedLiteralLikeTypeForContextualType and that calls getWidenedUniqueESSymbolType when there is no contextual type provided.

some issues that are (partially~) related:
https://github.com/microsoft/TypeScript/issues/32242
https://github.com/microsoft/TypeScript/issues/35896#issuecomment-569466046
https://github.com/microsoft/TypeScript/issues/36876

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 reproduce the emitted declaration for the example. Read getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded, getWidenedLiteralLikeTypeForContextualType, and getWidenedUniqueESSymbolType as identified in the issue. Done means the declaration preserves the explicit unique-symbol type as typeof test rather than widening fn's return type to symbol.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.