7
7
TSEnumDeclaration ,
8
8
TSExpressionWithTypeArguments ,
9
9
TSFunctionType ,
10
+ TSImportType ,
10
11
TSIndexedAccessType ,
11
12
TSInterfaceDeclaration ,
12
13
TSMappedType ,
@@ -168,6 +169,17 @@ function innerResolveTypeElements(
168
169
)
169
170
}
170
171
}
172
+ case 'TSImportType' :
173
+ const sourceScope = importSourceToScope (
174
+ ctx ,
175
+ node . argument ,
176
+ scope ,
177
+ node . argument . value
178
+ )
179
+ const resolved = resolveTypeReference ( ctx , node , sourceScope )
180
+ if ( resolved ) {
181
+ return resolveTypeElements ( ctx , resolved , resolved . _ownerScope )
182
+ }
171
183
}
172
184
return ctx . error ( `Unresolvable type: ${ node . type } ` , node , scope )
173
185
}
@@ -486,9 +498,14 @@ function resolveBuiltin(
486
498
}
487
499
}
488
500
501
+ type ReferenceTypes =
502
+ | TSTypeReference
503
+ | TSExpressionWithTypeArguments
504
+ | TSImportType
505
+
489
506
function resolveTypeReference (
490
507
ctx : TypeResolveContext ,
491
- node : ( TSTypeReference | TSExpressionWithTypeArguments ) & {
508
+ node : ReferenceTypes & {
492
509
_resolvedReference ?: ScopeTypeNode
493
510
} ,
494
511
scope ?: TypeScope ,
@@ -511,7 +528,7 @@ function innerResolveTypeReference(
511
528
ctx : TypeResolveContext ,
512
529
scope : TypeScope ,
513
530
name : string | string [ ] ,
514
- node : TSTypeReference | TSExpressionWithTypeArguments ,
531
+ node : ReferenceTypes ,
515
532
onlyExported : boolean
516
533
) : ScopeTypeNode | undefined {
517
534
if ( typeof name === 'string' ) {
@@ -555,11 +572,16 @@ function innerResolveTypeReference(
555
572
}
556
573
}
557
574
558
- function getReferenceName (
559
- node : TSTypeReference | TSExpressionWithTypeArguments
560
- ) : string | string [ ] {
561
- const ref = node . type === 'TSTypeReference' ? node . typeName : node . expression
562
- if ( ref . type === 'Identifier' ) {
575
+ function getReferenceName ( node : ReferenceTypes ) : string | string [ ] {
576
+ const ref =
577
+ node . type === 'TSTypeReference'
578
+ ? node . typeName
579
+ : node . type === 'TSExpressionWithTypeArguments'
580
+ ? node . expression
581
+ : node . qualifier
582
+ if ( ! ref ) {
583
+ return 'default'
584
+ } else if ( ref . type === 'Identifier' ) {
563
585
return ref . name
564
586
} else {
565
587
return qualifiedNameToPath ( ref )
@@ -599,27 +621,21 @@ type FS = NonNullable<SFCScriptCompileOptions['fs']>
599
621
600
622
function resolveTypeFromImport (
601
623
ctx : TypeResolveContext ,
602
- node : TSTypeReference | TSExpressionWithTypeArguments ,
624
+ node : ReferenceTypes ,
603
625
name : string ,
604
626
scope : TypeScope
605
627
) : ScopeTypeNode | undefined {
606
628
const { source, imported } = scope . imports [ name ]
607
- const resolved = resolveImportSource ( ctx , node , scope , source )
608
- return resolveTypeReference (
609
- ctx ,
610
- node ,
611
- fileToScope ( ctx , resolved ) ,
612
- imported ,
613
- true
614
- )
629
+ const sourceScope = importSourceToScope ( ctx , node , scope , source )
630
+ return resolveTypeReference ( ctx , node , sourceScope , imported , true )
615
631
}
616
632
617
- function resolveImportSource (
633
+ function importSourceToScope (
618
634
ctx : TypeResolveContext ,
619
635
node : Node ,
620
636
scope : TypeScope ,
621
637
source : string
622
- ) : string {
638
+ ) : TypeScope {
623
639
const fs : FS = ctx . options . fs || ts ?. sys
624
640
if ( ! fs ) {
625
641
ctx . error (
@@ -657,7 +673,7 @@ function resolveImportSource(
657
673
if ( resolved ) {
658
674
// (hmr) register dependency file on ctx
659
675
; ( ctx . deps || ( ctx . deps = new Set ( ) ) ) . add ( resolved )
660
- return normalizePath ( resolved )
676
+ return fileToScope ( ctx , normalizePath ( resolved ) )
661
677
} else {
662
678
return ctx . error (
663
679
`Failed to resolve import source ${ JSON . stringify ( source ) } .` ,
@@ -938,14 +954,13 @@ function recordTypes(
938
954
}
939
955
}
940
956
} else if ( stmt . type === 'ExportAllDeclaration' ) {
941
- const targetFile = resolveImportSource (
957
+ const sourceScope = importSourceToScope (
942
958
ctx ,
943
959
stmt . source ,
944
960
scope ,
945
961
stmt . source . value
946
962
)
947
- const targetScope = fileToScope ( ctx , targetFile )
948
- Object . assign ( scope . exportedTypes , targetScope . exportedTypes )
963
+ Object . assign ( scope . exportedTypes , sourceScope . exportedTypes )
949
964
}
950
965
}
951
966
}
@@ -1134,7 +1149,7 @@ export function inferRuntimeType(
1134
1149
return [ UNKNOWN_TYPE ]
1135
1150
}
1136
1151
1137
- case 'TSTypeReference' :
1152
+ case 'TSTypeReference' : {
1138
1153
const resolved = resolveTypeReference ( ctx , node , scope )
1139
1154
if ( resolved ) {
1140
1155
return inferRuntimeType ( ctx , resolved , resolved . _ownerScope )
@@ -1197,6 +1212,7 @@ export function inferRuntimeType(
1197
1212
}
1198
1213
// cannot infer, fallback to UNKNOWN: ThisParameterType
1199
1214
return [ UNKNOWN_TYPE ]
1215
+ }
1200
1216
1201
1217
case 'TSParenthesizedType' :
1202
1218
return inferRuntimeType ( ctx , node . typeAnnotation , scope )
@@ -1228,6 +1244,19 @@ export function inferRuntimeType(
1228
1244
case 'ClassDeclaration' :
1229
1245
return [ 'Object' ]
1230
1246
1247
+ case 'TSImportType' : {
1248
+ const sourceScope = importSourceToScope (
1249
+ ctx ,
1250
+ node . argument ,
1251
+ scope ,
1252
+ node . argument . value
1253
+ )
1254
+ const resolved = resolveTypeReference ( ctx , node , sourceScope )
1255
+ if ( resolved ) {
1256
+ return inferRuntimeType ( ctx , resolved , resolved . _ownerScope )
1257
+ }
1258
+ }
1259
+
1231
1260
default :
1232
1261
return [ UNKNOWN_TYPE ] // no runtime check
1233
1262
}
0 commit comments