#Infinite type instantiation around recursive conditional types

6 messages · Page 1 of 1 (latest)

warped warren
#

I got myself into a situation where using recursive conditional types causes TS to throw Type instantiation is excessively deep and possibly infinite.(2589):

type A<Q, S> = Q extends { a: any } ? A<Q["a"], S> : never;

interface B<T> {
  f<Q>(q: Q): A<Q, T>;
}

type C = B<{}> extends B<infer X> ? X : never;

type X = C extends any ? true : false;
//       ^ raises the error here

In my view, C is clearly {} and thus X should be true.

coarse juniperBOT
#
kartal8091#0

Preview:```ts
type A<Q, S> = Q extends {a: any}
? A<Q["a"], S>
: never

interface B<T> {
f<Q>(q: Q): A<Q, T>
}

type C = B<{}> extends B<infer X> ? X : never

type X = C extends any ? true : false```

sweet surge
#

if you infer the value of a it works (don't ask why, i'm not well-versed in the internals)

type A<Q, S> = Q extends { a: infer U } ? A<U, S> : never;
coarse juniperBOT
#

@warped warren Here's a shortened URL of your playground link! You can remove the full link from your message.

kartal8091#0

Preview:```ts
type A<Q, S> = Q extends {a: any}
? A<Q["a"], S>
: never

interface B<T> {
f<Q>(q: Q): A<Q, T>
}

declare function makeB<T>(t: T): B<T>

declare function f<T>(t: T): void

f<B<{}>>(makeB(null as any as {}))```

warped warren
#

So the type B here is generally difficult if not impossible to work with