Typescript throws an error with the following code:
type AndType<AstT> = { type: 'And', args: [AstT] };
type TrueType = { type: 'True' };
type BaseAST = AndType<BaseAST> | TrueType;
Complaining that Type alias 'BaseAST' circularly references itself.
; however, if I wrap the circular reference in an object, the types compile fine:
type AndType<AstT> = { type: 'And', args: [AstT] };
type TrueType = { type: 'True' };
type BaseAST = {value: AndType<BaseAST>} | {value: TrueType};
Why? Does anyone have a reference to the docs where this behavior is defined?
The easiest solution in your case is to make the AndType
type an interface as follows:
interface AndType<AstT> {
type: 'And';
args: [AstT];
}
type TrueType = { type: 'True' };
type BaseAST = AndType<BaseAST> | TrueType;
In general it's recommended to use interfaces for straightforward types, but this isn't always possible. The link already given should give you more in-depth information.
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