microsoft / microsoft/TypeScript

Symbols in const assertions should be considered unique

Open
#35,562 5 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

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 updated TypeScript Playground reproduction and confirm the TS1166 error for sealedDictionary.prop in the computed class property. Trace the type-checking path for const assertions and unique symbol property names, then add coverage showing that the sealedDictionary case is accepted without changing the rejected dictionary case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.