@@ -372,7 +372,8 @@ function createImportAdderWorker(sourceFile: SourceFile, program: Program, useAu
372
372
quotePreference ,
373
373
defaultImport ,
374
374
namedImports && arrayFrom ( namedImports . entries ( ) , ( [ name , addAsTypeOnly ] ) => ( { addAsTypeOnly, name } ) ) ,
375
- namespaceLikeImport ) ;
375
+ namespaceLikeImport ,
376
+ compilerOptions ) ;
376
377
newDeclarations = combine ( newDeclarations , declarations ) ;
377
378
} ) ;
378
379
if ( newDeclarations ) {
@@ -1249,7 +1250,13 @@ function codeActionForFixWorker(changes: textChanges.ChangeTracker, sourceFile:
1249
1250
const namespaceLikeImport = importKind === ImportKind . Namespace || importKind === ImportKind . CommonJS
1250
1251
? { importKind, name : qualification ?. namespacePrefix || symbolName , addAsTypeOnly }
1251
1252
: undefined ;
1252
- insertImports ( changes , sourceFile , getDeclarations ( moduleSpecifier , quotePreference , defaultImport , namedImports , namespaceLikeImport ) , /*blankLineBetween*/ true , preferences ) ;
1253
+ insertImports ( changes , sourceFile , getDeclarations (
1254
+ moduleSpecifier ,
1255
+ quotePreference ,
1256
+ defaultImport ,
1257
+ namedImports ,
1258
+ namespaceLikeImport ,
1259
+ compilerOptions ) , /*blankLineBetween*/ true , preferences ) ;
1253
1260
if ( qualification ) {
1254
1261
addNamespaceQualifier ( changes , sourceFile , qualification ) ;
1255
1262
}
@@ -1489,12 +1496,18 @@ function getNewImports(
1489
1496
quotePreference : QuotePreference ,
1490
1497
defaultImport : Import | undefined ,
1491
1498
namedImports : readonly Import [ ] | undefined ,
1492
- namespaceLikeImport : Import & { importKind : ImportKind . CommonJS | ImportKind . Namespace } | undefined
1499
+ namespaceLikeImport : Import & { importKind : ImportKind . CommonJS | ImportKind . Namespace } | undefined ,
1500
+ compilerOptions : CompilerOptions ,
1493
1501
) : AnyImportSyntax | readonly AnyImportSyntax [ ] {
1494
1502
const quotedModuleSpecifier = makeStringLiteral ( moduleSpecifier , quotePreference ) ;
1495
1503
let statements : AnyImportSyntax | readonly AnyImportSyntax [ ] | undefined ;
1496
1504
if ( defaultImport !== undefined || namedImports ?. length ) {
1497
- const topLevelTypeOnly = ( ! defaultImport || needsTypeOnly ( defaultImport ) ) && every ( namedImports , needsTypeOnly ) ;
1505
+ // `verbatimModuleSyntax` should prefer top-level `import type` -
1506
+ // even though it's not an error, it would add unnecessary runtime emit.
1507
+ const topLevelTypeOnly = ( ! defaultImport || needsTypeOnly ( defaultImport ) ) && every ( namedImports , needsTypeOnly ) ||
1508
+ compilerOptions . verbatimModuleSyntax &&
1509
+ defaultImport ?. addAsTypeOnly !== AddAsTypeOnly . NotAllowed &&
1510
+ ! some ( namedImports , i => i . addAsTypeOnly === AddAsTypeOnly . NotAllowed ) ;
1498
1511
statements = combine ( statements , makeImport (
1499
1512
defaultImport && factory . createIdentifier ( defaultImport . name ) ,
1500
1513
namedImports ?. map ( ( { addAsTypeOnly, name } ) => factory . createImportSpecifier (
0 commit comments