microsoft / microsoft/TypeScript

Inconsistent behavior with Generic ElementType

Đang mở
#61,995 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Domain: JSX/TSX Help Wanted Possible Improvement
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔎 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_

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ .tsx với tsconfig.json được liên kết trên TypeScript 5.8.3 và phiên bản phát triển 5.9, so sánh JSX với dsl.createElement và chẩn đoán TS2786. Theo dõi cách JSX.ElementType kiểm tra generic Form; được xem là hoàn tất khi dạng JSX biên dịch nhất quán với lời gọi đã được desugar, giữ nguyên kiểu Element được suy luận theo mong muốn và tránh lỗi tên không xác định gây hiểu lầm.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.