microsoft / microsoft/TypeScript

Suggestion: treat readonly static members of class as namespace members

Aperta
#38,803 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi dell'issue e con l'issue correlata #38207, quindi segui il modo in cui TypeScript gestisce i membri static readonly, i membri di namespace e le posizioni di tipo. Il lavoro è completo quando un enum o una classe aliasato tramite un membro static readonly può essere usato sia come valore sia come tipo, senza la dichiarazione di namespace dettagliata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.