microsoft / microsoft/TypeScript

Inconsistent behavior with Generic ElementType

Aperta
#61,995 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.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 Search Terms

JSX, Generic ElementType, TS2786

### 🕗 Version & Regression Information

- Behavior seen in 5.8.3 and 5.9.0-dev.20250703
- I was unable to test this on prior versions

### ⏯ Playground Link

_No response_

### 💻 Code

```ts
export namespace dsl {
export namespace JSX {
export type Element = {
name: U
type: string
children: Element[]
}

[tsconfig.json](https://github.com/user-attachments/files/21047590/tsconfig.json)

export function createElement(
tag: JSX.ElementType,
props : { name: U },
...children: JSX.Element[]
): JSX.Element {
return tag(props, ...children)
}
}

function Form(props: { name: U }, ...children: dsl.JSX.Element[]) {
return { ...props, type: "form", children }
}

const formDesugared = dsl.createElement(
Form, { name: "one" }
)

const form = (
// <--- Error here, error TS2786: 'Form' cannot be used as a JSX component.
)

```

### 🙁 Actual behavior

Compile error:

sui.tsx:31:6 - error TS2786: 'Form' cannot be used as a JSX component.
Its type '(props: { name: U; }, ...children: Element[]) => { type: string; children: Element[]; name: U; }' is not a valid JSX element type.
Types of parameters 'props' and 'props' are incompatible.
Type '{ name: unknown; }' is not assignable to type '{ name: string; }'.
Types of property 'name' are incompatible.
Type 'unknown' is not assignable to type 'string'.

31

### 🙂 Expected behavior

Changing props: { name: U } in the definition of ElementType to props: { name: string } causes the code to compile [loosing the wanted typing of the resulting Element

Since the desugared version of the code compiles and infers types correctly, I'd expect the JSX code to compile, or at least produce a less deeply mysterious error - the type of the 'name' property plainly *isn't* unknown.

### Additional information about the issue

_No response_

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 .tsx con il tsconfig.json collegato in TypeScript 5.8.3 e nella versione di sviluppo 5.9, confrontando JSX con dsl.createElement e la diagnostica TS2786. Traccia il modo in cui JSX.ElementType verifica il generico Form; il lavoro è completato quando la forma JSX compila in modo coerente con la chiamata desugared, preserva la tipizzazione Element inferita prevista ed evita l'errore fuorviante di nome sconosciuto.

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

Valutazione

Stack tecnologico
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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.