Type inference worked in 4.2.3 but fails in 4.3.2

Đang mở
#44,412 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.

Đánh giá

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

Hướng nghiên cứu

Bắt đầu với repro TypeScript Playground được liên kết và so sánh việc suy luận giữa các phiên bản 4.2.3 và 4.3.2, tập trung vào lựa chọn null trong mảng choices và lệnh gọi promptChoice. Được xem là hoàn tất khi ví dụ không còn báo lỗi gán tuple và choice được suy luận là 'one'|'two'|'three'.

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

Mô tả

Bug Domain: check: Type Inference

Bug Report

🔎 Search Terms

type inference

🕗 Version & Regression Information

4.3.2

  • This is a crash
    [x] This changed between versions 4.2.3 and 4.3.2
  • 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 _______
⏯ Playground Link

Playground link with relevant code

💻 Code

(strict null checks off)

interface PromptChoiceOptions<T extends string> {
	label: string;
	button: string;
	default?: T;
	choices: readonly ( T | [ string, T ] )[];
}

function promptChoice<T extends string>( options?: Partial<PromptChoiceOptions<T>> ) {
    return options?.choices?.[ 0 ] ?? null; // simplified
}

const choice = promptChoice( { label: 'example', button: 'select', choices: [
    [ 'null', null ], // problem seems to be related to this line
    [ 'One', 'one' ],
    [ 'Two', 'two' ],
    [ 'Three', 'three' ]
] } );
🙁 Actual behavior

Type [string, "two"] is not assignable to type [string, "one"]

🙂 Expected behavior

choice is inferred to be 'one'|'two'|'three'


In creating a minimal repro for this, I discovered that commenting out the null choice makes the code behave properly. Also, changing the type of choices to readonly ( T | [ string, T|null] )[]; with strict null checking makes the problem always occur.

Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

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.

Issue khác của microsoft/TypeScript

Tất cả issue của microsoft/TypeScript

Issue tương tự

Thêm issue về Go

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.