Why does this work:
type Foo = { x: Foo }
but this doesn't:
type Bar<A> = { x: A }
type Foo = Bar<Foo>
// ^^^ Type alias 'Foo' circularly references itself
Shouldn't they be equivalent?
Per the documentation, a type alias can refer to itself in a property, but not anywhere else on the right side of the declaration:
We can also have a type alias refer to itself in a property
So, as you noted, this works:
type Foo = { x: Foo }
However, it’s not possible for a type alias to appear anywhere else on the right side of the declaration
But this fails:
type Foo = Bar<Foo>
Well, the second fails even if you don't use the constraint:
type Bar<A> = { x: string }
type Foo = Bar<Foo> // still fails with the same message
Basically, it the 2nd form needs to be this:
type Bar = { x: Foo }
type Foo = Bar;
To be equivalent to the first one.
Also, with strictNullChecks
I'm pretty sure that there's no way to initialize such a type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With