microsoft / microsoft/TypeScript

Improve error message if JSX factory function is not imported

Aperta
#44,618 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experience Enhancement Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

A colleague who is new to JSX recently wrote code equivalent to <div></div> (Playground link) and got this confusing error:

JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
Cannot find name 'React'.

But from my colleague's perspective, why would TypeScript be looking for "name 'React'"? It is not obvious from reading the code that it compiles to React.createElement('div') and that this requires an import.

I can imagine two possible solutions:

  1. Improve the error message, e.g. "JSX requires importing a factory function (configured as React.createElement in this project) but that import is not present in this file."
  2. Make the import unnecessary, e.g. introduce a new option to tell the compiler how to automatically add an import for the factory function if it's needed.

This is different than, but in the same general category as, other issues about JSX error messages like #42955 and #42794. I suspect this issue might not get enough priority to be addressed on its own, but maybe it could in the context of a small effort to improve JSX error messages in general.

🔎 Search Terms

JSX error message

🕗 Version & Regression Information

As far as I know, this has always happened. It at least happens in TS 4.3.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSX.
⏯ Playground Link

Playground link with relevant code

💻 Code
<div></div>
🙁 Actual behavior

Received confusing error message described above.

🙂 Expected behavior

Received clear error message or no error.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio Playground collegato e lo snippet JSX per riprodurre i diagnostic attuali, inclusi gli errori mancanti di JSX.IntrinsicElements e React. Il lavoro è completato quando l’esempio produce un diagnostic chiaro di factory-import o non richiede più l’import, con il comportamento scelto riflesso nei compiler tests pertinenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
react, typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
28/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.