Investigating long TypeScript compile times

Seems I have found the culprit in my case. I did it the hard way; my process:

  1. Find the commit that made the compilation slow. Go through the history commit-by-commit and check the compile times.
  2. Comment out the changed code until the offending lines are found.

Before the offending commit, I consistently got compile times of around 2-4 seconds, after the commit – 13-17 seconds.

In my case, I have a class, with a accessTokenGetter field, which was initialized in the constructor:

export class JwtConfig {
    //...
    accessTokenGetter: () => Observable<string>;
    //...
      constructor(private config?: IJwtConfig) {
          // ...
          this.accessTokenGetter = this.config.accessTokenGetter || (() => Observable.of(null));
      }
}

The second part of the initialization || (() => Observable.of(null)); was causing the slowness. Commenting it out or adding a type annotation got the compile time back down. Since Observable is generic, it seems like the TypeScript compiler needs a hint to narrow down some type checks it needs to do. My initialization now reads as:

//...
this.accessTokenGetter = this.config.accessTokenGetter || (() => Observable.of(<string>null));
//...

Observable.of(null as string)) also seems to do the job. There were a couple of other places where adding a type annotation sped compilation up.

Hope this helps someone.

Still, if there’s a facility in the compiler to the answer faster – I’d be happy to hear it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)