microsoft / microsoft/TypeScript

Allow JSX element tags to be symbols

Abierto
#38,367 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

Search Terms

es6 symbols JSX createElement element tag type intrinsic host

Suggestion

Allow JSX element tags to be ES6 symbols instead of just strings, functions or classes.

Use Cases

JSX-based libraries like React have adopted the convention of using special upper-cased symbols as the tag of the JSX expression (<Fragment />, <Suspense />) as a way to provide intrinsic elements which aren’t callable and whose behavior are defined by the library rather than the tag.

Currently, TypeScript only allows JSX tags to be strings, functions or classes as defined by the JSX interface, and will throw errors like the following if Symbols are used:

error TS2604: JSX element type 'Fragment' does not have any construct or call signatures.

Libraries are therefore forced to lie about the actual typings of Symbol tags as something other than a unique symbol. For instance, React defines Symbol tags as ExoticComponent and pretends that the symbols are callable (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L358-L364). The framework I work on (Crank.js) just defines symbols meant to be used as tags as any but this then has further repercussions when I attempt to, for instance, use these symbol types in interfaces.

Symbols are useful because they:

  1. Allow intrinsic elements to be upper-cased. This allows people to provide renderer-provided element APIs which mirror XML languages which are case-sensitive.
  2. Not globally scoped, so there is no possibility of namespace clashes, and can be imported/exported.
  3. Can still work with the global IntrinsicElements interfaces to be strongly typed.

Examples

const Fragment = Symbol.for("crank.Fragment");
type Fragment = typeof Fragment;

const Portal = Symbol.for("crank.Portal");
type Portal = typeof Portal;

// should not error
const el = (
  <Fragment>{children}</Fragment>
);

declare namespace JSX {
    interface IntrinsicElements {
        foo: any;
        [Portal]: {root: any};
    }
}

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.

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

Los puntos de entrada del issue son la comprobación de tipos de elementos JSX y las declaraciones de IntrinsicElements del namespace JSX; comienza siguiendo cómo se validan los casos existentes de cadenas, funciones y clases. Compara los ejemplos de Fragment y Portal con el diagnóstico TS2604 actual y define pruebas para JSX con etiquetas de símbolo y claves de símbolo de IntrinsicElements; el trabajo estará terminado cuando se acepten las etiquetas de símbolo válidas sin cambiar el JavaScript emitido.

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

Evaluación

Stack tecnológico
javascript, typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.