microsoft / microsoft/TypeScript

Suggestion: treat readonly static members of class as namespace members

Offen
#38,803 3 Kommentare 1 Reaktion 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
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Search Terms

static member; namespace; type

Maybe related issues: https://github.com/microsoft/TypeScript/issues/38207

Suggestion

Readonly static members of class should act like namespace members. Especially, when that member is assigned as an alias of enumeration or other class.

Use Cases

With the suggestion,

enum Color { red, }
class Image {
    public static readonly Color = Color; // Marked as readonly, so tsc can ensure it's an alias
}

The Image.Color should be an alias of enumeration Color. So we can use it when an enumeration may be used:

console.log(Image.Color.red); // OK
function f(color: Image.Color) { // OK: used as type #1
    if (color === Image.Color.red){ /* ... */ }
}

#1 is invalid for now because tsc does not treat Image.Color as a type.
To use Image.Color as a type, we may supply a verbose declaration according to the declaration merging rule of TypeScript:

namespace Image {
    export type Color = (typeof Color)[keyof typeof Color]; // :(
}

It's verbose...

Examples

Sometimes we want to embed enumerations, classes into other classes. Because we don't want directly expose that enumueration/class into module scope.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 den Beispielen des Issues und dem verwandten Issue #38207 und verfolge dann, wie TypeScript static readonly-Mitglieder, Namespace-Mitglieder und Typpositionen behandelt. Als abgeschlossen gilt, dass ein Enum oder eine Klasse, das bzw. die über ein readonly static-Mitglied aliasiert ist, sowohl als Wert als auch als Typ verwendet werden kann, ohne die ausführliche Namespace-Deklaration.

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
25/100

Neue Issues direkt in Ihr Postfach

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