#How to solve "Type instantiation is excessively deep and possibly infinite."

7 messages · Page 1 of 1 (latest)

young sail
#

Explain the issue

I worked on a project for serialization and tried my best to strictly check the parameters passing from the user, so there's a deep check for nested data.
But the recursive check gives me the "Type instantiation is excessively deep and possibly infinite." error, and I have no idea how to solve this.

TypeScript Playground

https://shorturl.at/cgxU1

limpid violet
#

here's a reduced version of that problem to help you see what's going on:

cold zephyrBOT
#
mkantor#0

Preview:```ts
type OptionType = {
option: Schema
}

type Schema = OptionType

type Nil = null | undefined

type Value<S extends Schema> = S extends OptionType
? Value<S["option"]> | Nil
: unknown

type Repro = Value<Schema>```

limpid violet
#

unfortunately i have to run so can't help further right now, but hopefully that will help. if nobody else comes along after a while feel free to use the !helper command

limpid violet
#

i'm back and played around with your code a bit more. one way to avoid the infinite recursion issue is to introduce a self-imposed depth limit, like so:

#

there may be other/better approaches, that's just the first thing i tried that worked