@@ -36,10 +36,11 @@ import type {
36
36
RestElement ,
37
37
TypeScript ,
38
38
TSQualifiedName ,
39
+ TSLiteralType ,
39
40
} from '@babel/types' ;
40
41
import { getDocblock } from './docblock.js' ;
41
42
42
- const tsTypes = {
43
+ const tsTypes : Record < string , string > = {
43
44
TSAnyKeyword : 'any' ,
44
45
TSBooleanKeyword : 'boolean' ,
45
46
TSUnknownKeyword : 'unknown' ,
@@ -54,7 +55,13 @@ const tsTypes = {
54
55
TSVoidKeyword : 'void' ,
55
56
} ;
56
57
57
- const namedTypes = {
58
+ const namedTypes : Record <
59
+ string ,
60
+ (
61
+ path : NodePath < any > ,
62
+ typeParams : TypeParameters | null ,
63
+ ) => TypeDescriptor | null
64
+ > = {
58
65
TSArrayType : handleTSArrayType ,
59
66
TSTypeReference : handleTSTypeReference ,
60
67
TSTypeLiteral : handleTSTypeLiteral ,
@@ -67,6 +74,7 @@ const namedTypes = {
67
74
TSTypeQuery : handleTSTypeQuery ,
68
75
TSTypeOperator : handleTSTypeOperator ,
69
76
TSIndexedAccessType : handleTSIndexedAccessType ,
77
+ TSLiteralType : handleTSLiteralType ,
70
78
} ;
71
79
72
80
function handleTSQualifiedName (
@@ -85,6 +93,15 @@ function handleTSQualifiedName(
85
93
return { name : printValue ( path ) . replace ( / < .* > $ / , '' ) } ;
86
94
}
87
95
96
+ function handleTSLiteralType ( path : NodePath < TSLiteralType > ) : LiteralType {
97
+ const literal = path . get ( 'literal' ) ;
98
+
99
+ return {
100
+ name : 'literal' ,
101
+ value : printValue ( literal ) ,
102
+ } ;
103
+ }
104
+
88
105
function handleTSArrayType (
89
106
path : NodePath < TSArrayType > ,
90
107
typeParams : TypeParameters | null ,
@@ -98,7 +115,7 @@ function handleTSArrayType(
98
115
99
116
function handleTSTypeReference (
100
117
path : NodePath < TSTypeReference > ,
101
- typeParams : TypeParameters ,
118
+ typeParams : TypeParameters | null ,
102
119
) : TypeDescriptor < TSFunctionSignatureType > | null {
103
120
let type : TypeDescriptor < TSFunctionSignatureType > ;
104
121
const typeName = path . get ( 'typeName' ) ;
@@ -508,16 +525,9 @@ function getTSTypeWithResolvedTypes(
508
525
}
509
526
510
527
if ( node . type in tsTypes ) {
511
- type = { name : tsTypes [ node . type ] } ;
512
- } else if ( path . isTSLiteralType ( ) ) {
513
- const literal = path . get ( 'literal' ) ;
514
-
515
- type = {
516
- name : 'literal' ,
517
- value : printValue ( literal ) ,
518
- } ;
528
+ type = { name : tsTypes [ node . type ] ! } ;
519
529
} else if ( node . type in namedTypes ) {
520
- type = namedTypes [ node . type ] ( path , typeParams ) ;
530
+ type = namedTypes [ node . type ] ! ( path , typeParams ) ;
521
531
}
522
532
523
533
if ( ! type ) {
0 commit comments