microsoft / microsoft/TypeScript
API: add function to resolve global symbol
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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
valueDeclarationmust be aNamedDeclarationwith aComputedPropertyName - get the type of the computed property name to obtain a reference to the
(unique) symboltype getApparentTypeto obtain a reference to the globalSymboltype- its
symbolshould hopefully always refer to the globalSymbolconstructor
- find a property whose name starts with
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par la TypeChecker API et les cas particuliers existants des déclarations globales décrits dans l’issue, notamment Array, Object, Function, ReadonlyArray et Symbol. Déterminez comment un point d’entrée public getGlobalSymbol s’intégrerait à l’API du checker et ajoutez une couverture pour la résolution des déclarations globales. Le travail est considéré comme terminé lorsque les consommateurs peuvent obtenir de manière fiable le symbole du constructeur global Symbol sans les contournements listés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100