microsoft / microsoft/TypeScript

Symbols in const assertions should be considered unique

Offen
#35,562 5 Kommentare 13 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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:

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der aktualisierten TypeScript Playground-Reproduktion und bestätige den TS1166-Fehler für sealedDictionary.prop in der berechneten Klasseneigenschaft. Verfolge den Typprüfpfad für const assertions und unique symbol-Eigenschaftsnamen und füge anschließend eine Abdeckung hinzu, die zeigt, dass der sealedDictionary-Fall akzeptiert wird, ohne den abgelehnten dictionary-Fall zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.