Home > Enterprise >  Typescript - using "infer" keyword on generic type with multiple multiple generic paramete
Typescript - using "infer" keyword on generic type with multiple multiple generic paramete

Time:03-16

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'."

CodePudding user response:

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.

  • Related