I'm dealing with two distinct types of string literals:
type X = {
type: "A1",
value: number
} | {
type: "A2",
value: string
};
type Y = {
type: "A1",
test: (value: number) => void;
} | {
type: "A2",
test: (value: string) => void;
}
Within the resolve() function, the goal is to invoke the test() method using the value property from U type arguments:
let resolve = (u: X, v: Y) => {
if (u.type === v.type) {
v.test(u.value)
}
}
An issue arises with the error message:
Argument of type 'string | number' is not assignable to parameter of type 'number & string'.
Type 'string' is not assignable to type 'number & string'.
Type 'string' is not assignable to type 'number'.
To address this, I had to introduce a workaround by segregating the type checks as follows:
let resolve = (u: X, v: Y) => {
if ((u.type === "A1" && v.type === "A1")) {
v.test(u.value)
} else if (u.type === "A2" && v.type === "A2") {
v.test(u.value)
}
}
The question now remains - is there a way to declare a function without explicitly checking each literal type?