#How can I tell whether my recursive types are guaranteed to work?

3 messages · Page 1 of 1 (latest)

gilded cradle
#

I'm trying to define a set of types that can refer to the whole set (the union of all those types), in a way that the set can be changed without having to rewrite each type.
See the piece of code which follows to understand what I mean.

The other day I tried something more or less similar, but I ran into weird issues: TS was arbitrarily reporting errors in some cases but not others.
By "arbitrarily" I mean that by looking at the code, I can't tell when or why it would report errors.
I learned that it's due to https://github.com/microsoft/TypeScript/issues/37426: apparently only some recursion approaches are supported by TS. The others may work or may not work depending on the TS' internals, in a way that one can't predict.

I wonder whether the approach I'm using in the following snippet is guaranteed to work.
In case some approach might fail in some cases (due to not being supported by TS), I'd like to know and I'd like to find ways to make it always work.
How can I tell?

midnight templeBOT
#
_bluenebula_#0

Preview:```ts
type Graph = {Types: unknown; Term: unknown}

type Term<G extends Graph> = {
id: "Term"
value: G["Term"]
}
type List<G extends Graph> = {
id: "List"
elemType: G["Types"]
}
type BinTree<G extends Graph> = {
id: "BinTree"
leftType: G["Types"]
rightType: G["Types"]
}
...```

gilded cradle
#

In case it's impossible to tell whether it's a supported approach, what should I expect, if I start using it?

  • Is it possible that my types work with the current version of TS, but spits out error in the next?
  • Is it possible that errors pop up in some graphs by changing things seemingly unrelated to the graph?

The unpredictable errors seem to be due to the type checker deferring evaluation in some cases but not others. I'd like to understand what could affect the type checker's behavior, in case that my recursive approach is not supported.