@@ -73,7 +73,7 @@ export interface Options {
7373 */
7474interface Cache {
7575 contents : { [ path : string ] : string }
76- versions : { [ path : string ] : number }
76+ versions : { [ path : string ] : number | undefined }
7777 outputs : { [ path : string ] : string }
7878}
7979
@@ -224,9 +224,7 @@ export function register (options: Options = {}): Register {
224224
225225 // Add all files into the file hash.
226226 for ( const fileName of config . fileNames ) {
227- if ( / \. d \. t s $ / . test ( fileName ) ) {
228- cache . versions [ fileName ] = 1
229- }
227+ cache . versions [ fileName ] = 1
230228 }
231229
232230 /**
@@ -281,14 +279,25 @@ export function register (options: Options = {}): Register {
281279 if ( typeCheck ) {
282280 // Set the file contents into cache.
283281 const setCache = function ( code : string , fileName : string ) {
284- cache . contents [ fileName ] = code
285- cache . versions [ fileName ] = ( cache . versions [ fileName ] + 1 ) || 1
282+ if ( cache . contents [ fileName ] !== code ) {
283+ cache . contents [ fileName ] = code
284+ cache . versions [ fileName ] = ( cache . versions [ fileName ] || 0 ) + 1
285+ }
286286 }
287287
288288 // Create the compiler host for type checking.
289289 const serviceHost = {
290290 getScriptFileNames : ( ) => Object . keys ( cache . versions ) ,
291- getScriptVersion : ( fileName : string ) => String ( cache . versions [ fileName ] ) ,
291+ getScriptVersion : ( fileName : string ) => {
292+ const version = cache . versions [ fileName ]
293+
294+ // We need to return `undefined` and not a string here because TypeScript will use
295+ // `getScriptVersion` and compare against their own version - which can be `undefined`.
296+ // If we don't return `undefined` it results in `undefined === "undefined"` and run
297+ // `createProgram` again (which is very slow). Using a `string` assertion here to avoid
298+ // TypeScript errors from the function signature (expects `(x: string) => string`).
299+ return version === undefined ? undefined as any as string : String ( version )
300+ } ,
292301 getScriptSnapshot ( fileName : string ) {
293302 if ( ! cache . contents [ fileName ] ) {
294303 if ( ! fileExists ( fileName ) ) {
0 commit comments