microsoft / microsoft/TypeScript
JSX broken generics with polymorphic components
Open
Nobody has claimed this yet.
Domain: JSX/TSX
Help Wanted
Possible Improvement
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the generic PolymorphicComponent case with the non-generic Component case. Trace how JSX checks the polymorphic component and verify completion by making the generic example accept className without an error while preserving the existing non-generic behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100