microsoft / microsoft/TypeScript

API: add function to resolve global symbol

Offen
#42,947 0 Kommentare 0 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

Suggestion

🔍 Search Terms

symbol, constructor, well known, global

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Expose getGlobalSymbol on TypeChecker

📃 Motivating Example

TypeScript applies some special handling to global declarations, e.g. Array, Object, Function, ReadonlyArray,
The recent change to well known Symbols adds yet another special case for the global Symbol value symbol.

Currently there's no reliable way to obtain the symbol of a global declaration.

💻 Use Cases

To find out whether a type is iterable, I need to look for a __@iterable property. Previously that worked well with a hard coded string. Now I need to look up the global Symbol constructor type, get the type of its property iterator and use the escapedName for the lookup on the type in question. The first step is actually the tricky one, because there's no API for it.

There are a few workarounds I can think of:

  • the hacky way: checker.getSymbolsInScope('' as any, ts.SymbolFlags.Value).filter(s => s.escapedName === 'Symbol')
  • the not so obvious way:
    • find a property whose name starts with __@ since those are symbol names
    • its valueDeclaration must be a NamedDeclaration with a ComputedPropertyName
    • get the type of the computed property name to obtain a reference to the (unique) symbol type
    • getApparentType to obtain a reference to the global Symbol type
    • its symbol should hopefully always refer to the global Symbol constructor

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 TypeChecker API und den im Issue beschriebenen bestehenden Sonderfällen für globale Deklarationen, einschließlich Array, Object, Function, ReadonlyArray und Symbol. Ermittle, wie sich ein öffentlicher getGlobalSymbol-Einstiegspunkt in die Checker-API einfügen würde, und füge Abdeckung für die Auflösung globaler Deklarationen hinzu. Als erledigt gilt die Arbeit, wenn Verbraucher zuverlässig das globale Symbol des Symbol-Konstruktors ohne die aufgeführten Workarounds erhalten können.

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

Neue Issues direkt in Ihr Postfach

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