'Typescript - using "infer" keyword on generic type with multiple multiple generic parameters not working as expected
Let's say I have following type:
type Foo<A, B> = A & B;
When I do this:
type Bar<T> = T extends Foo<infer _, infer K> ? K : never;
I expect that this would work:
const someVariable: Bar<Foo<string, number>> = 200;
But I am getting the following error: "Type 'number' is not assignable to type 'never'."
Solution 1:[1]
type Foo<A, B> = A & B;
be careful with passing primitive like string and number because
type Foo<A, B> = A & B;
type R = Foo<string, boolean>;
R is never
Probably you should change to type Foo<A, B> = A | B; it should solve your issue.
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 | Egor |
