microsoft / microsoft/TypeScript

Merging constants with namespaces

Offen
#18,163 13 Kommentare 16 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

I wrote this in closed #13536 first, but thought it might get lost there, so filing a new issue.

Merging consts with namespaces really makes sense. For a real-world example, let's take a popular library: TinyMCE, writing proper type definitions for which requires this feature. Namespaces are really convenient in this case because all the classes from the API are attached to the tinymce namespace object or to its subnamespaces. In the API, there are normal classes like tinymce.Editor and so called 'static classes' like tinymce.EditorManager which are just a type (interface) plus an object value of this type attached to the namespace:

declare namespace tinymce {
  // normal class
  class Editor {
    show(): void;
    // ...
  }
  // "static class"
  interface EditorManager {
    activeEditor: Editor;
    // ...
  }
  const EditorManager: EditorManager;
  // ...
}

Nothing unusual so far, but there is a plot twist. The namespace object tinymce itself implements the tinymce.EditorManager interface. It could be easily and beautifully solved by merging a const with the namespace:

declare const tinymce: tinymce.EditorManager;
declare namespace tinymce {
  // ... see the previous snippet
}

But unfortunately this isn't allowed. The error message is: Cannot redeclare block-scoped variable 'tinymce'.

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 damit, die tinymce-Deklarationsausschnitte zu reproduzieren und das Verhalten beim Zusammenführen von Deklarationen zu lesen, das den gemeldeten Fehler wegen einer erneuten Deklaration im Block-Scope erzeugt. Als erledigt gilt die Aufgabe, wenn const und Namespace für dieses Beispiel zusammengeführt werden können, ohne bestehende Deklarationsregeln zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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