microsoft / microsoft/TypeScript
Regression in some error elaborations involving a target type mixing arrays with non-arrays
Abierto
@ahejlsberg ya está trabajando en esto.
Desde el 20/8/2025.
Needs Investigation
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
🔎 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?
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Evaluación
Este issue todavía no se ha evaluado.