-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Description
π Search Terms
discriminated discrimination array error elaboration single
π Version & Regression Information
- This changed in PR Port "Improve type discrimination algorithm" from tsgoΒ #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: #61828 (comment)
- 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?
davidkpiano
Metadata
Metadata
Assignees
Labels
No labels