@@ -18,6 +18,7 @@ import {
18
18
convertParameterNodes ,
19
19
convertTypeParameterNodes ,
20
20
} from "./factories/signature" ;
21
+ import { convertSymbol } from "./symbols" ;
21
22
22
23
export function convertJsDocAlias (
23
24
context : Context ,
@@ -33,6 +34,15 @@ export function convertJsDocAlias(
33
34
return ;
34
35
}
35
36
37
+ // If the typedef tag is just referring to another type-space symbol, with no type parameters
38
+ // or appropriate forwarding type parameters, then we treat it as a re-export instead of creating
39
+ // a type alias with an import type.
40
+ const aliasedSymbol = getTypedefReExportTarget ( context , declaration ) ;
41
+ if ( aliasedSymbol ) {
42
+ convertSymbol ( context , aliasedSymbol , exportSymbol ?? symbol ) ;
43
+ return ;
44
+ }
45
+
36
46
const reflection = context . createDeclarationReflection (
37
47
ReflectionKind . TypeAlias ,
38
48
symbol ,
@@ -165,3 +175,58 @@ function convertTemplateParameterNodes(
165
175
const params = ( nodes ?? [ ] ) . flatMap ( ( tag ) => tag . typeParameters ) ;
166
176
return convertTypeParameterNodes ( context , params ) ;
167
177
}
178
+
179
+ function getTypedefReExportTarget (
180
+ context : Context ,
181
+ declaration : ts . JSDocTypedefTag | ts . JSDocEnumTag
182
+ ) : ts . Symbol | undefined {
183
+ const typeExpression = declaration . typeExpression ;
184
+ if (
185
+ ! ts . isJSDocTypedefTag ( declaration ) ||
186
+ ! typeExpression ||
187
+ ts . isJSDocTypeLiteral ( typeExpression ) ||
188
+ ! ts . isImportTypeNode ( typeExpression . type ) ||
189
+ ! typeExpression . type . qualifier ||
190
+ ! ts . isIdentifier ( typeExpression . type . qualifier )
191
+ ) {
192
+ return ;
193
+ }
194
+
195
+ const targetSymbol = context . expectSymbolAtLocation (
196
+ typeExpression . type . qualifier
197
+ ) ;
198
+ const decl = targetSymbol . declarations ?. [ 0 ] ;
199
+
200
+ if (
201
+ ! decl ||
202
+ ! (
203
+ ts . isTypeAliasDeclaration ( decl ) ||
204
+ ts . isInterfaceDeclaration ( decl ) ||
205
+ ts . isJSDocTypedefTag ( decl ) ||
206
+ ts . isJSDocCallbackTag ( decl )
207
+ )
208
+ ) {
209
+ return ;
210
+ }
211
+
212
+ const targetParams = ts . getEffectiveTypeParameterDeclarations ( decl ) ;
213
+ const localParams = ts . getEffectiveTypeParameterDeclarations ( declaration ) ;
214
+ const localArgs = typeExpression . type . typeArguments || [ ] ;
215
+
216
+ // If we have type parameters, ensure they are forwarding parameters with no transformations.
217
+ // This doesn't check constraints since they aren't checked in JSDoc types.
218
+ if (
219
+ targetParams . length !== localParams . length ||
220
+ localArgs . some (
221
+ ( arg , i ) =>
222
+ ! ts . isTypeReferenceNode ( arg ) ||
223
+ ! ts . isIdentifier ( arg . typeName ) ||
224
+ arg . typeArguments ||
225
+ localParams [ i ] ?. name . text !== arg . typeName . text
226
+ )
227
+ ) {
228
+ return ;
229
+ }
230
+
231
+ return targetSymbol ;
232
+ }
0 commit comments