microsoft / microsoft/TypeScript

JSX broken generics with polymorphic components

Open
#56,314 4 comments 1 reaction 0 assignees View on GitHub

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

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgIilQ3wChSkAPSWOTAVwDsNgJG4AFCAGwE8RoYABbA0AYVyRGSRjAA8AQQDOcKjBkATFQHINwAG7a4AHzjaURgLxm9hgHwAKAN6kAkCiUAuOBPBsZMAA0pAC+3i7uXnDKoQCUcBEA9IlwgDLkcAAqIipKQhD03BpwAO7QANZwKIxFAEZIQij6SHAwQs1KKCDNHnB13BDFbkQw9FDscr5SAXBo3B5KAHKdSJbaRol2bsmZvGDN2k4zc0qLy95KMFDAjADmANxwIUbAKowQ8PPAN4woNdzNMAgLV2+wAMsAalAUFBeABZKooG5IDQKGCXCH0dRKOQAUX+XVkgR8xyUqPRNUxSGxAAkMrDQQARAx4pAEmB2OAAMjgtPpZKuFKxcl5jOZ+ICHO5YhJ-IxQpFCmYeSgLLZkuiSugCrRAsp2IAdIa7HZtKFyKRtgAhTEtNpwDpdErlFTATBwN7wehKZGVFQoOBI6RXNCkNBsC4+ST+WRway6AxGHrxwwmMwWUPh+AZHEAZQysbgEyj0hjs3mSy6qyxMHWdiAA

💻 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.