@@ -506,6 +506,14 @@ namespace ts.server {
506506 }
507507 abstract getTypeAcquisition ( ) : TypeAcquisition ;
508508
509+ protected removeLocalTypingsFromTypeAcquisition ( newTypeAcquisition : TypeAcquisition ) : TypeAcquisition {
510+ if ( ! newTypeAcquisition || ! newTypeAcquisition . include ) {
511+ // Nothing to filter out, so just return as-is
512+ return newTypeAcquisition ;
513+ }
514+ return { ...newTypeAcquisition , include : this . removeExistingTypings ( newTypeAcquisition . include ) } ;
515+ }
516+
509517 getExternalFiles ( ) : SortedReadonlyArray < string > {
510518 return emptyArray as SortedReadonlyArray < string > ;
511519 }
@@ -718,7 +726,8 @@ namespace ts.server {
718726 this . projectStateVersion ++ ;
719727 }
720728
721- private extractUnresolvedImportsFromSourceFile ( file : SourceFile , result : Push < string > ) {
729+ /* @internal */
730+ private extractUnresolvedImportsFromSourceFile ( file : SourceFile , result : Push < string > , ambientModules : string [ ] ) {
722731 const cached = this . cachedUnresolvedImportsPerFile . get ( file . path ) ;
723732 if ( cached ) {
724733 // found cached result - use it and return
@@ -731,7 +740,7 @@ namespace ts.server {
731740 if ( file . resolvedModules ) {
732741 file . resolvedModules . forEach ( ( resolvedModule , name ) => {
733742 // pick unresolved non-relative names
734- if ( ! resolvedModule && ! isExternalModuleNameRelative ( name ) ) {
743+ if ( ! resolvedModule && ! isExternalModuleNameRelative ( name ) && ! isAmbientlyDeclaredModule ( name ) ) {
735744 // for non-scoped names extract part up-to the first slash
736745 // for scoped names - extract up to the second slash
737746 let trimmed = name . trim ( ) ;
@@ -748,6 +757,10 @@ namespace ts.server {
748757 } ) ;
749758 }
750759 this . cachedUnresolvedImportsPerFile . set ( file . path , unresolvedImports || emptyArray ) ;
760+
761+ function isAmbientlyDeclaredModule ( name : string ) {
762+ return ambientModules . some ( m => m === name ) ;
763+ }
751764 }
752765
753766 /**
@@ -777,8 +790,9 @@ namespace ts.server {
777790 // 4. compilation settings were changed in the way that might affect module resolution - drop all caches and collect all data from the scratch
778791 if ( hasChanges || changedFiles . length ) {
779792 const result : string [ ] = [ ] ;
793+ const ambientModules = this . program . getTypeChecker ( ) . getAmbientModules ( ) . map ( mod => stripQuotes ( mod . getName ( ) ) ) ;
780794 for ( const sourceFile of this . program . getSourceFiles ( ) ) {
781- this . extractUnresolvedImportsFromSourceFile ( sourceFile , result ) ;
795+ this . extractUnresolvedImportsFromSourceFile ( sourceFile , result , ambientModules ) ;
782796 }
783797 this . lastCachedUnresolvedImportsList = toDeduplicatedSortedArray ( result ) ;
784798 }
@@ -804,6 +818,13 @@ namespace ts.server {
804818 return ! hasChanges ;
805819 }
806820
821+
822+
823+ protected removeExistingTypings ( include : string [ ] ) : string [ ] {
824+ const existing = ts . getAutomaticTypeDirectiveNames ( this . getCompilerOptions ( ) , this . directoryStructureHost ) ;
825+ return include . filter ( i => existing . indexOf ( i ) < 0 ) ;
826+ }
827+
807828 private setTypings ( typings : SortedReadonlyArray < string > ) : boolean {
808829 if ( arrayIsEqualTo ( this . typingFiles , typings ) ) {
809830 return false ;
@@ -1299,7 +1320,7 @@ namespace ts.server {
12991320 }
13001321
13011322 setTypeAcquisition ( newTypeAcquisition : TypeAcquisition ) : void {
1302- this . typeAcquisition = newTypeAcquisition ;
1323+ this . typeAcquisition = this . removeLocalTypingsFromTypeAcquisition ( newTypeAcquisition ) ;
13031324 }
13041325
13051326 getTypeAcquisition ( ) {
@@ -1445,7 +1466,7 @@ namespace ts.server {
14451466 Debug . assert ( ! ! newTypeAcquisition . include , "newTypeAcquisition.include may not be null/undefined" ) ;
14461467 Debug . assert ( ! ! newTypeAcquisition . exclude , "newTypeAcquisition.exclude may not be null/undefined" ) ;
14471468 Debug . assert ( typeof newTypeAcquisition . enable === "boolean" , "newTypeAcquisition.enable may not be null/undefined" ) ;
1448- this . typeAcquisition = newTypeAcquisition ;
1469+ this . typeAcquisition = this . removeLocalTypingsFromTypeAcquisition ( newTypeAcquisition ) ;
14491470 }
14501471 }
14511472}
0 commit comments