microsoft / microsoft/TypeScript

Component not always a valid JSX component depending on a generic type

Aperta
#54,702 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: JSX/TSX Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

jsx react generic valid JSX

🕗 Version & Regression Information
  • This changed between versions 5.0.4 and 5.1.3
⏯ Playground Link

Playground Link

💻 Code
import * as React from "react";

const A = <T extends unknown>({ a }: { a: T }) => (typeof a === "string" ? null : new Error("a must be a string")) as T extends string ? null : Error;

const jsxWithError = <A<string> a="a" />;

const B = A<string>;

const jsxWithoutError = <B a="a" />;

🙁 Actual behavior

Error on A in jsxWithError:

'A' cannot be used as a JSX component.
  Its type '<T extends unknown>({ a }: { a: T; }) => T extends string ? null : Error' is not a valid JSX element type.
    Type '<T extends unknown>({ a }: { a: T; }) => T extends string ? null : Error' is not assignable to type '(props: any, deprecatedLegacyContext?: any) => ReactNode'.
      Type 'Error' is not assignable to type 'ReactNode'.(2786)
🙂 Expected behavior

No error with the generic type provided (as is the case for jsxWithoutError), since we are sure we are using a valid JSX component.

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

Riproduci l’esempio Playground collegato in TypeScript 5.0.4 e 5.1.3, confrontando l’uso generico JSX di A con quello specializzato di B. Traccia la diagnostica 2786 e assicurati che la forma generica con un argomento di tipo esplicito venga accettata, mentre il caso di errore esistente rimanga significativo.

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

Valutazione

Stack tecnologico
react, typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.