Microsoft/TypeScript
GitHub ã§èŠãJSX broken generics with polymorphic components
Open
#56,314 opened on 2023幎11æ5æ¥
Domain: JSX/TSXHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð 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