@@ -3478,6 +3478,40 @@ export class Parser extends DiagnosticEmitter {
34783478 return null ;
34793479 }
34803480
3481+ private getRecursiveDepthForTypeDeclaration (
3482+ identifierName : string ,
3483+ type : TypeNode ,
3484+ depth : i32 = 0
3485+ ) : i32 {
3486+ switch ( type . kind ) {
3487+ case NodeKind . NAMEDTYPE : {
3488+ let typeArguments = ( < NamedTypeNode > type ) . typeArguments ;
3489+ if ( typeArguments ) {
3490+ for ( let i = 0 , k = typeArguments . length ; i < k ; i ++ ) {
3491+ let res = this . getRecursiveDepthForTypeDeclaration ( identifierName , typeArguments [ i ] , depth + 1 ) ;
3492+ if ( res != - 1 ) return res ;
3493+ }
3494+ }
3495+ if ( ( < NamedTypeNode > type ) . name . identifier . text == identifierName ) {
3496+ return depth ;
3497+ }
3498+ break ;
3499+ }
3500+ case NodeKind . FUNCTIONTYPE : {
3501+ let fnType = < FunctionTypeNode > type ;
3502+ let res = this . getRecursiveDepthForTypeDeclaration ( identifierName , fnType . returnType , depth + 1 ) ;
3503+ if ( res != - 1 ) return res ;
3504+ let params = fnType . parameters ;
3505+ for ( let i = 0 , k = params . length ; i < k ; i ++ ) {
3506+ res = this . getRecursiveDepthForTypeDeclaration ( identifierName , params [ i ] . type , depth + 1 ) ;
3507+ if ( res != - 1 ) return res ;
3508+ }
3509+ break ;
3510+ }
3511+ }
3512+ return - 1 ;
3513+ }
3514+
34813515 parseTypeDeclaration (
34823516 tn : Tokenizer ,
34833517 flags : CommonFlags ,
@@ -3499,6 +3533,21 @@ export class Parser extends DiagnosticEmitter {
34993533 tn . skip ( Token . BAR ) ;
35003534 let type = this . parseType ( tn ) ;
35013535 if ( ! type ) return null ;
3536+ let depth = this . getRecursiveDepthForTypeDeclaration ( name . text , type ) ;
3537+ if ( depth >= 0 ) {
3538+ if ( depth == 0 ) {
3539+ this . error (
3540+ DiagnosticCode . Type_alias_0_circularly_references_itself ,
3541+ tn . range ( ) , name . text
3542+ ) ;
3543+ } else {
3544+ this . error (
3545+ DiagnosticCode . Not_implemented_0 ,
3546+ tn . range ( ) , "Recursion in type aliases"
3547+ ) ;
3548+ }
3549+ return null ;
3550+ }
35023551 let ret = Node . createTypeDeclaration (
35033552 name ,
35043553 decorators ,
0 commit comments