microsoft / microsoft/TypeScript
Regression in some error elaborations involving a target type mixing arrays with non-arrays
Đang mở
@ahejlsberg đang làm issue này rồi.
Từ ngày 20/8/2025.
Needs Investigation
- 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
discriminated discrimination array error elaboration single
🕗 Version & Regression Information
- This changed in PR https://github.com/microsoft/TypeScript/pull/61828
⏯ Playground Link
💻 Code
type SingleOrArray<T> = T | T[];
type Item =
| {
src: "foo";
}
| {
src: "bar";
};
const obj1: SingleOrArray<Item> = {
src: "baz", // ✅ errors nicely at property
};
// ⚠️ no change but reported error is at suboptimal position
const obj2: SingleOrArray<Item> | undefined = {
src: "baz",
};
// ⚠️ no change but reported error is at suboptimal position
const obj3: SingleOrArray<Item | undefined> = {
src: "baz",
};
// ⚠️ no change but reported error is at suboptimal position
const obj4: SingleOrArray<Item | undefined> | undefined = {
src: "baz",
};
const arr1: SingleOrArray<Item> = {
src: "baz", // ✅ errors nicely at property
};
const arr2: SingleOrArray<Item> | undefined = [
{
src: "baz", // ✅ errors nicely at property
},
];
const arr3: SingleOrArray<Item | undefined> = [
{
src: "baz", // ✅ errors nicely at property
},
];
const arr4: SingleOrArray<Item | undefined> | undefined = [
{
src: "baz", // ✅ errors nicely at property
},
];
type Logic<I, O> = (input: I) => O;
type Item2 =
| {
src: "foo";
id: string;
}
| {
src: Logic<never, unknown>;
id?: never;
};
declare const logic: Logic<string, number>;
// ❌ error moved from a property to the variable
const obj5: SingleOrArray<Item2> = {
src: logic,
id: "someId",
};
// ❌ error moved from a property to the variable
const obj6: SingleOrArray<Item2> | undefined = {
src: logic,
id: "someId",
};
// ❌ error moved from a property to the variable
const obj7: SingleOrArray<Item2 | undefined> = {
src: logic,
id: "someId",
};
// ❌ error moved from a property to the variable
const obj8: SingleOrArray<Item2 | undefined> | undefined = {
src: logic,
id: "someId",
};
// ⚠️ no change but reported error is at suboptimal position
const arr5: SingleOrArray<Item2> = [
{
src: logic,
id: "someId",
},
];
// ⚠️ no change but reported error is at suboptimal position
const arr6: SingleOrArray<Item2> | undefined = [
{
src: logic,
id: "someId",
},
];
const arr7: SingleOrArray<Item2 | undefined> = [
{
src: logic,
id: "someId", // ✅ errors nicely at property
},
];
const arr8: SingleOrArray<Item2 | undefined> | undefined = [
{
src: logic,
id: "someId", // ✅ errors nicely at property
},
];
🙁 Actual behavior
Some of the errors in the "second group" moved. It is a specific situation with multiple available discriminants.
🙂 Expected behavior
I'd really expect all of the errors within each "group" to be reported at a property. I especially don't see a reason why | undefined would make the error move to one of the parent nodes.
Additional information about the issue
this was caught here: https://github.com/microsoft/TypeScript/pull/61828#issuecomment-2957137115
- is this purely an elaboration issue?
- or could a heuristic be introduced that would allow for array targets to be eliminated early when discriminating by available object members?
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Đánh giá
Issue này chưa được đánh giá.