microsoft / microsoft/TypeScript
Error messaging improvements when accidentally missing an = in JSX attributes
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
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} />
}
🙁 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.
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 le TypeScript JSX playground fourni et la reproduction dans Workbench d’un = manquant entre des attributs JSX, puis examinez comment le diagnostic actuel '...' expected est produit. Le travail est terminé lorsque la même erreur continue de produire une erreur, mais fournit une suggestion utile pour ajouter le = manquant sans présenter à tort l’expression comme un spread.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- react, typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 38/100