'The intersection 'xxx & xxx' was reduced to 'never' because property 'xxx' has conflicting types in some constituents
I got an error while using ts generics, here is the simple code:
On the last line, ts reports the following error:
error TS2345: Argument of type 'Task<"build"> | Task<"repair">' is not assignable to parameter of type 'never'.
The intersection 'Task<"build"> & Task<"repair">' was reduced to 'never' because property 'type' has conflicting types in some constituents.
44 action_map[task.type].execute(task);
I try to use switch to avoid errors:
function execute<T extends TaskType>(task: Task<T>) {
switch (task.type) {
case "build":
// now type of `task` should be `Task<"build">`
BuildAction.execute(task);
break;
case "repair":
// now type of `task` should be `Task<"repair">`
RepairAction.execute(task);
break;
default:
// now type of `task` should be `Task<never>`
console.log("Error");
}
}
But it even worse:
error TS2345: Argument of type 'Task<T>' is not assignable to parameter of type 'Task<"build">'.
Type 'T' is not assignable to type '"build"'.
Type 'keyof TMap' is not assignable to type '"build"'.
Type '"repair"' is not assignable to type '"build"'.
50 BuildAction.execute(task);
I noticed that vscode's type hint for task is always Task<T> instead of what I expected.
So, what should I do?
Solution 1:[1]
The fastest way is to transform both in sets and print the difference:
>>> print(set(x).difference(set(y)))
{6}
This code print numbers present in x but not in y
Solution 2:[2]
x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
for i in x:
if i in y:
print(f"{i} found")
else:
print(f"{i} not found")
Solution 3:[3]
This is the best option in my opinion.
x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
for number in x:
if number not in y:
print(f"{number} not found")
Solution 4:[4]
to get not matches:
def returnNotMatches(a, b):
return [[x for x in a if x not in b], [x for x in b if x not in a]]
or
new_list = list(set(list1).difference(list2))
to get the intersection:
list1 =[1,2,3,4,5,6]
list2 = [1,2,3,4,5]
list1_as_set = set(list1)
intersection = list1_as_set.intersection(list2)
output:
{1, 2, 3, 4, 5}
you can also transfer it to a list:
intersection_as_list = list(intersection)
or:
new_list = list(set(list1).intersection(list2))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joao Donasolo |
| Solution 2 | Robin Sage |
| Solution 3 | Key27 |
| Solution 4 |
