microsoft / microsoft/TypeScript

Error messaging improvements when accidentally missing an = in JSX attributes

Abierto
#42,955 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Bug Report

I think we could give better errors when you miss an = between JSX attributes. Coming from this tweet.

🔎 Search Terms

JSX spread expected attribute

🕗 Version & Regression Information

N/A - Tested in nightly

⏯ Playground Link
import React from "react"

const shouldDisable = true
const b = () => {
    <div aria-disabled {shouldDisable} />
//                      ^^^^^^^^^^^^^ error: '...' expected.(1005)
}

The gist is that TypeScript thinks you will always be using {thing} as a spread {...thing} - but it's also just possible that you missed the =. In this case specifically, shouldDisable is a boolean, which can never spread.

Two ways we could safely improve:

Checking the type of the value in the { }:

// In this case `aria-disabled` is a boolean, and so including it here defaults to 'true' (and doesn't error)

// shouldDisable is a boolean which can never spread like current error: '...' expected.(1005)
// TS could check if token before is a jsx identifier and then recommend to add the `=`
// error: "'shouldDisable' cannot be spread, did you mean to write 'aria-disabled={shoudlDisable}"?
const shouldDisable = true
const b = () => {
    <div aria-disabled {shouldDisable} />
}

Checking whether the identifier matches an attribute:

import React from "react"
const onFocus = () => {}

// onFocus known to be function, so this 2nd error:  '...' expected.(1005)
// TS could first look to see if there's an attribute with the same name and recommend:
// error: "'onFocus' is not being used to spread inside the JSX element, did you mean to write 'onFocus={onFocus}"?
const a = () => {
    <div onFocus {onFocus} />
}


Workbench Repro

🙁 Actual behavior

It's always an error and assumed to be a spread but without the spread

🙂 Expected behavior

It'd still be an error, but it could be an error which provides the next step.

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 el TypeScript JSX playground proporcionado y la reproducción en Workbench de un = ausente entre atributos JSX; después, inspecciona cómo se genera actualmente el diagnóstico '...' expected. Se considera terminado cuando el mismo error siga produciendo un error, pero proporcione una sugerencia útil para añadir el = ausente sin representar incorrectamente la expresión como un spread.

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
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.