@@ -20,6 +20,7 @@ class Entry {
2020
2121export default class DtsGenerator {
2222 private _extractor : Extractor ;
23+ private _typeChecker : ts . TypeChecker ;
2324 private readonly _entriesBySymbol : Map < ts . Symbol , Entry > = new Map < ts . Symbol , Entry > ( ) ;
2425 private readonly _entries : Entry [ ] = [ ] ;
2526
@@ -84,6 +85,7 @@ export default class DtsGenerator {
8485
8586 public constructor ( extractor : Extractor ) {
8687 this . _extractor = extractor ;
88+ this . _typeChecker = extractor . typeChecker ;
8789 }
8890
8991 /**
@@ -100,7 +102,7 @@ export default class DtsGenerator {
100102 public generateDtsFileContent ( ) : string {
101103 const packageSymbol : ts . Symbol = this . _extractor . package . getDeclarationSymbol ( ) ;
102104
103- const exportSymbols : ts . Symbol [ ] = this . _extractor . typeChecker . getExportsOfModule ( packageSymbol ) || [ ] ;
105+ const exportSymbols : ts . Symbol [ ] = this . _typeChecker . getExportsOfModule ( packageSymbol ) || [ ] ;
104106 for ( const exportSymbol of exportSymbols ) {
105107
106108 const entry : Entry = this . _fetchEntryForSymbol ( exportSymbol ) ;
@@ -124,7 +126,7 @@ export default class DtsGenerator {
124126 }
125127
126128 private _fetchEntryForSymbol ( symbol : ts . Symbol ) : Entry {
127- const followedSymbol : ts . Symbol = DtsGenerator . _followAliases ( symbol , this . _extractor . typeChecker ) ;
129+ const followedSymbol : ts . Symbol = DtsGenerator . _followAliases ( symbol , this . _typeChecker ) ;
128130
129131 const entry : Entry = this . _createEntry ( symbol . name , followedSymbol ) ;
130132
@@ -137,6 +139,10 @@ export default class DtsGenerator {
137139 switch ( declaration . kind ) {
138140 case ts . SyntaxKind . ClassDeclaration :
139141 this . _processClassDeclaration ( entry , declaration as ts . ClassDeclaration ) ;
142+ break ;
143+ case ts . SyntaxKind . InterfaceDeclaration :
144+ this . _processInterfaceDeclaration ( entry , declaration as ts . InterfaceDeclaration ) ;
145+ break ;
140146 }
141147 }
142148
@@ -146,7 +152,40 @@ export default class DtsGenerator {
146152 private _processClassDeclaration ( entry : Entry , declaration : ts . ClassDeclaration ) : void {
147153 const classDts : dts . ClassDeclaration = dts . create . class ( entry . localName ) ;
148154 entry . dtsDeclarations . push ( classDts ) ;
149- // classDts.implements.push(interfaceDts);
155+ }
156+
157+ private _processInterfaceDeclaration ( entry : Entry , declaration : ts . InterfaceDeclaration ) : void {
158+ const interfaceDts : dts . InterfaceDeclaration = dts . create . interface ( entry . localName ) ;
159+ entry . dtsDeclarations . push ( interfaceDts ) ;
160+
161+ const type : ts . Type = this . _typeChecker . getTypeAtLocation ( declaration ) ;
162+ const baseTypes : ts . TypeReference [ ] = type . getBaseTypes ( ) as ts . TypeReference [ ] ;
163+ for ( const baseType of baseTypes ) {
164+ const baseDts : dts . NamedTypeReference = this . _getTypeReferenceDts ( baseType ) ;
165+ interfaceDts . baseTypes . push ( baseDts ) ;
166+ }
167+ }
168+
169+ private _getTypeReferenceDts ( typeReference : ts . TypeReference ) : dts . NamedTypeReference {
170+ if ( ! typeReference . symbol ) {
171+ const intrinsicName : string = typeReference [ 'intrinsicName' ] ; // tslint:disable-line:no-string-literal
172+ if ( intrinsicName ) {
173+ // It is a simple primitive type
174+ return dts . create . namedTypeReference ( intrinsicName ) ;
175+ } else {
176+ throw new Error ( 'Unimplemented type reference:\r\n' + this . _typeChecker . typeToString ( typeReference ) ) ;
177+ }
178+ } else {
179+ const entry : Entry = this . _fetchEntryForSymbol ( typeReference . symbol ) ;
180+ const referenceDts : dts . NamedTypeReference = dts . create . namedTypeReference ( entry . localName ) ;
181+
182+ for ( const argument of typeReference . typeArguments || [ ] ) {
183+ const renderedArgument : dts . NamedTypeReference = this . _getTypeReferenceDts ( argument as ts . TypeReference ) ;
184+ referenceDts . typeArguments . push ( renderedArgument ) ;
185+ }
186+
187+ return referenceDts ;
188+ }
150189 }
151190
152191 private _createEntry ( localName : string , followedSymbol : ts . Symbol ) : Entry {
0 commit comments