microsoft / microsoft/TypeScript
JSX broken generics with polymorphic components
Aperta
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
🔎 Search Terms
"jsx generics", "polymorhic components", "react jsx"
🕗 Version & Regression Information
- This is a crash No
- This changed between versions -
- This changed in commit or PR -
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about -
- I was unable to test this on prior versions because - all versions in TS playground have the same output
⏯ Playground Link
💻 Code
import React from "react"
export function PolymorphicComponent<As extends 'div' | 'a' = 'div'>({
as: Component,
}: {
as: As
}) {
// ❌ This should work and behave the same as below
return <Component className='' />
// Type '{ className: string; }' is not assignable to type 'LibraryManagedAttributes<Element, ClassAttributes<HTMLDivElement> & HTMLAttributes<HTMLDivElement> & ClassAttributes<HTMLAnchorElement> & AnchorHTMLAttributes<...>>'
}
// But the same works if not used as a generic
const Component = 'div' as 'div' | 'a'
const TEST = <Component className='test' />
🙁 Actual behavior
Shows an error.
🙂 Expected behavior
No error.
Additional information about the issue
No response
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dalla riproduzione collegata in TypeScript Playground e confronta il caso generico PolymorphicComponent con il caso non generico Component. Traccia il modo in cui JSX verifica il componente polimorfico e verifica il completamento facendo sì che l'esempio generico accetti className senza errori, preservando al contempo il comportamento non generico esistente.
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
- 35/100