microsoft / microsoft/TypeScript

API: add function to resolve global symbol

Abierto
#42,947 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la TypeChecker API y los casos especiales existentes de declaraciones globales descritos en el issue, incluidos Array, Object, Function, ReadonlyArray y Symbol. Determina cómo encajaría un punto de entrada público getGlobalSymbol en la API del checker y añade cobertura para resolver declaraciones globales. Se considera terminado cuando los consumidores puedan obtener de forma fiable el símbolo del constructor global Symbol sin las soluciones alternativas indicadas.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.