microsoft / microsoft/TypeScript

Use React TSX tags without having to write "import React from 'react'"

Abierto
#27,235 11 comentarios 5 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

Suggestion

I have to write import React from 'react' every time I use TSX syntax even though I make no explicit mention of the name React in the following lines of code (Of course, TSC output will contain mentions of React but the key is explicit).

It is possible to write JSX tags in Babel's JavaScript without import React from 'react' by using Babel's/Webpack's plugins. A notable example is Next.js.

Therefore I suggest: If user are not mentioning React directly, user shouldn't have to declare React explicitly. This can be configured in compiler options, something like a combination of --jsxFactoryPath and --jsxModule for example.

Examples

React.js
Ideal Source Code

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "jsxFactoryPath": ["createElement"],
    "jsxModule": "react"
  }
}

index.tsx:

export = <div id='123'>hello</div>
Desired Output

index.js:

var _jsx = require("react").createElement;
module.exports = _jsx("div", { id: "123" }, "hello");
Hyperscript
Ideal Source Code

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "jsxFactoryPath": [],
    "jsxModule": "hyperscript"
  }
}

index.tsx:

export = <div id='123'>hello</div>
Desired Output
var _jsx = require("hyperscript");
module.exports = _jsx("div", { id: "123" }, "hello");
Generic
Ideal Source Code

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "jsxFactoryPath": ["obj1", "obj2", "func"],
    "jsxModule": "pkg/dir1/dir2"
  }
}

index.tsx:

export = <div id='123'>hello</div>
Desired Output

index.js:

var _jsx = require("pkg/dir1/dir2").obj1.obj2.func;
module.exports = _jsx("div", { id: "123" }, "hello");

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. new expression-level syntax)

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 las opciones del compilador mostradas en tsconfig.json y los ejemplos de TSX en index.tsx; después, compara el comportamiento de Babel/Webpack al que se hace referencia en el issue. Se considera terminado cuando TSX puede usar un módulo y una factory configurables sin un import explícito de React, y produce la salida CommonJS solicitada para los ejemplos de React, hyperscript y genérico.

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

Evaluación

Stack tecnológico
react, 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.