[SOLVED] Typescript – using "infer" keyword on generic type with multiple multiple generic parameters not working as expected

Issue

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

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.

Answered By – Egor

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *