Issue Description:
When defining a union type that includes objects with various properties, TypeScript struggles to determine the correct type for error messages, especially when some types have multiple discriminating keys.
I believe I may be making an error in my approach, but I am aiming to improve the clarity of error messages for function calls in this scenario.
Snippet:
interface X1 {
name: "X";
id: 1;
category: "one" | "two" | "three";
}
interface X2 {
name: "X";
id: 2;
category: "one" | "two" | "three" | "four" | "five";
}
interface Y {
name: "Y";
id: 1;
section: "alpha" | "beta" | "gamma";
}
interface Z {
title: "Z";
template: "template" | "test";
}
type Items = X1 | X2 | Y | Z;
async function processItem({ name, ...data }: Items): Promise<boolean> {
// perform operation.
return true;
}
processItem({
name: "X",
id: 2,
category: "one",
});
Live Example:
Expected Error Message for the incorrect 'processItem' call:
"'one' is not a valid option for 'category', choose from 'one' | 'two' | 'three' | 'four' | 'five'" (correctly indicating the issue lies in the category property of X2 type.)
Actual Error Message Received:
"Argument of type '{ name: "X"; id: 2; category: "one"; }' is not assignable to parameter of type 'Items'. Type '{ name: "X"; id: 2; category: "one"; }' is not assignable to type 'X1 | X2 | Y | Z'. Types of property 'id' are incompatible. Type '2' is not assignable to type '1'."