@@ -602,6 +602,7 @@ namespace ts.Completions {
602
602
propertyAccessToConvert : PropertyAccessExpression | undefined ,
603
603
isJsxInitializer : IsJsxInitializer | undefined ,
604
604
importCompletionNode : Node | undefined ,
605
+ isTypeOnlyImport : boolean ,
605
606
useSemicolons : boolean ,
606
607
options : CompilerOptions ,
607
608
preferences : UserPreferences ,
@@ -661,7 +662,7 @@ namespace ts.Completions {
661
662
if ( originIsResolvedExport ( origin ) ) {
662
663
sourceDisplay = [ textPart ( origin . moduleSpecifier ) ] ;
663
664
if ( importCompletionNode ) {
664
- ( { insertText, replacementSpan } = getInsertTextAndReplacementSpanForImportCompletion ( name , importCompletionNode , origin , useSemicolons , options , preferences ) ) ;
665
+ ( { insertText, replacementSpan } = getInsertTextAndReplacementSpanForImportCompletion ( name , importCompletionNode , isTypeOnlyImport , origin , useSemicolons , options , preferences ) ) ;
665
666
isSnippet = preferences . includeCompletionsWithSnippetText ? true : undefined ;
666
667
}
667
668
}
@@ -746,7 +747,7 @@ namespace ts.Completions {
746
747
} ;
747
748
}
748
749
749
- function getInsertTextAndReplacementSpanForImportCompletion ( name : string , importCompletionNode : Node , origin : SymbolOriginInfoResolvedExport , useSemicolons : boolean , options : CompilerOptions , preferences : UserPreferences ) {
750
+ function getInsertTextAndReplacementSpanForImportCompletion ( name : string , importCompletionNode : Node , isTypeOnly : boolean | undefined , origin : SymbolOriginInfoResolvedExport , useSemicolons : boolean , options : CompilerOptions , preferences : UserPreferences ) {
750
751
const sourceFile = importCompletionNode . getSourceFile ( ) ;
751
752
const replacementSpan = createTextSpanFromNode ( importCompletionNode , sourceFile ) ;
752
753
const quotedModuleSpecifier = quote ( sourceFile , preferences , origin . moduleSpecifier ) ;
@@ -756,12 +757,13 @@ namespace ts.Completions {
756
757
ExportKind . Named ;
757
758
const tabStop = preferences . includeCompletionsWithSnippetText ? "$1" : "" ;
758
759
const importKind = codefix . getImportKind ( sourceFile , exportKind , options , /*forceImportKeyword*/ true ) ;
760
+ const typeOnlyPrefix = isTypeOnly ? ` ${ tokenToString ( SyntaxKind . TypeKeyword ) } ` : " " ;
759
761
const suffix = useSemicolons ? ";" : "" ;
760
762
switch ( importKind ) {
761
- case ImportKind . CommonJS : return { replacementSpan, insertText : `import ${ escapeSnippetText ( name ) } ${ tabStop } = require(${ quotedModuleSpecifier } )${ suffix } ` } ;
762
- case ImportKind . Default : return { replacementSpan, insertText : `import ${ escapeSnippetText ( name ) } ${ tabStop } from ${ quotedModuleSpecifier } ${ suffix } ` } ;
763
- case ImportKind . Namespace : return { replacementSpan, insertText : `import * as ${ escapeSnippetText ( name ) } from ${ quotedModuleSpecifier } ${ suffix } ` } ;
764
- case ImportKind . Named : return { replacementSpan, insertText : `import { ${ escapeSnippetText ( name ) } ${ tabStop } } from ${ quotedModuleSpecifier } ${ suffix } ` } ;
763
+ case ImportKind . CommonJS : return { replacementSpan, insertText : `import${ typeOnlyPrefix } ${ escapeSnippetText ( name ) } ${ tabStop } = require(${ quotedModuleSpecifier } )${ suffix } ` } ;
764
+ case ImportKind . Default : return { replacementSpan, insertText : `import${ typeOnlyPrefix } ${ escapeSnippetText ( name ) } ${ tabStop } from ${ quotedModuleSpecifier } ${ suffix } ` } ;
765
+ case ImportKind . Namespace : return { replacementSpan, insertText : `import${ typeOnlyPrefix } * as ${ escapeSnippetText ( name ) } from ${ quotedModuleSpecifier } ${ suffix } ` } ;
766
+ case ImportKind . Named : return { replacementSpan, insertText : `import${ typeOnlyPrefix } { ${ escapeSnippetText ( name ) } ${ tabStop } } from ${ quotedModuleSpecifier } ${ suffix } ` } ;
765
767
}
766
768
}
767
769
@@ -814,6 +816,7 @@ namespace ts.Completions {
814
816
const start = timestamp ( ) ;
815
817
const variableDeclaration = getVariableDeclaration ( location ) ;
816
818
const useSemicolons = probablyUsesSemicolons ( sourceFile ) ;
819
+ const isTypeOnlyImport = ! ! importCompletionNode && isTypeOnlyImportOrExportDeclaration ( location . parent ) ;
817
820
// Tracks unique names.
818
821
// Value is set to false for global variables or completions from external module exports, because we can have multiple of those;
819
822
// true otherwise. Based on the order we add things we will always see locals first, then globals, then module exports.
@@ -844,6 +847,7 @@ namespace ts.Completions {
844
847
propertyAccessToConvert ,
845
848
isJsxInitializer ,
846
849
importCompletionNode ,
850
+ isTypeOnlyImport ,
847
851
useSemicolons ,
848
852
compilerOptions ,
849
853
preferences
@@ -1916,6 +1920,7 @@ namespace ts.Completions {
1916
1920
1917
1921
function isTypeOnlyCompletion ( ) : boolean {
1918
1922
return insideJsDocTagTypeExpression
1923
+ || ! ! importCompletionNode && isTypeOnlyImportOrExportDeclaration ( location . parent )
1919
1924
|| ! isContextTokenValueLocation ( contextToken ) &&
1920
1925
( isPossiblyTypeArgumentPosition ( contextToken , sourceFile , typeChecker )
1921
1926
|| isPartOfTypeNode ( location )
0 commit comments