Speed improvements

TypeScript 3.5 introduces several optimizations around type-checking and incremental builds.

Type-checking speed-ups

TypeScript 3.5 contains certain optimizations over TypeScript 3.4 for type-checking more efficiently. These improvements are significantly more pronounced in editor scenarios where type-checking drives operations like code completion lists.

--incremental improvements

TypeScript 3.5 improves on 3.4’s --incremental build mode, by saving information about how the state of the world was calculated - compiler settings, why files were looked up, where files were found, etc. In scenarios involving hundreds of projects using TypeScript’s project references in --build mode, we’ve found that the amount of time rebuilding can be reduced by as much as 68% compared to TypeScript 3.4!

For more details, you can see the pull requests to

The Omit helper type

TypeScript 3.5 introduces the new Omit helper type, which creates a new type with some properties dropped from the original.

  1. type Person = {
  2. name: string;
  3. age: number;
  4. location: string;
  5. };
  6. type QuantumPerson = Omit<Person, "location">;
  7. // equivalent to
  8. type QuantumPerson = {
  9. name: string;
  10. age: number;
  11. };

Here we were able to copy over all the properties of Person except for location using the Omit helper.

For more details, see the pull request on GitHub to add Omit, as well as the change to use Omit for object rest.

Improved excess property checks in union types

In TypeScript 3.4 and earlier, certain excess properties were allowed in situations where they really shouldn’t have been. For instance, TypeScript 3.4 permitted the incorrect name property in the object literal even though its types don’t match between Point and Label.

  1. type Point = {
  2. x: number;
  3. y: number;
  4. };
  5. type Label = {
  6. name: string;
  7. };
  8. const thing: Point | Label = {
  9. x: 0,
  10. y: 0,
  11. name: true // uh-oh!
  12. };

Previously, a non-disciminated union wouldn’t have any excess property checking done on its members, and as a result, the incorrectly typed name property slipped by.

In TypeScript 3.5, the type-checker at least verifies that all the provided properties belong to some union member and have the appropriate type, meaning that the sample above correctly issues an error.

Note that partial overlap is still permitted as long as the property types are valid.

  1. const pl: Point | Label = {
  2. x: 0,
  3. y: 0,
  4. name: "origin" // okay
  5. };

The --allowUmdGlobalAccess flag

In TypeScript 3.5, you can now reference UMD global declarations like

  1. export as namespace foo;

from anywhere - even modules - using the new --allowUmdGlobalAccess flag.

This mode adds flexibility for mixing and matching the way 3rd party libraries, where globals that libraries declare can always be consumed, even from within modules.

For more details, see the pull request on GitHub.

Smarter union type checking

In TypeScript 3.4 and prior, the following example would fail:

  1. type S = { done: boolean; value: number };
  2. type T = { done: false; value: number } | { done: true; value: number };
  3. declare let source: S;
  4. declare let target: T;
  5. target = source;

That’s because S isn’t assignable to { done: false, value: number } nor { done: true, value: number }. Why? Because the done property in S isn’t specific enough - it’s boolean whereas each constituent of T has a done property that’s specifically true or false. That’s what we meant by each constituent type being checked in isolation: TypeScript doesn’t just union each property together and see if S is assignable to that. If it did, some bad code could get through like the following:

  1. interface Foo {
  2. kind: "foo";
  3. value: string;
  4. }
  5. interface Bar {
  6. kind: "bar";
  7. value: number;
  8. }
  9. function doSomething(x: Foo | Bar) {
  10. if (x.kind === "foo") {
  11. x.value.toLowerCase();
  12. }
  13. }
  14. // uh-oh - luckily TypeScript errors here!
  15. doSomething({
  16. kind: "foo",
  17. value: 123
  18. });

However, this was a bit overly strict for the original example. If you figure out the precise type of any possible value of S, you can actually see that it matches the types in T exactly.

In TypeScript 3.5, when assigning to types with discriminant properties like in T, the language actually will go further and decompose types like S into a union of every possible inhabitant type. In this case, since boolean is a union of true and false, S will be viewed as a union of { done: false, value: number } and { done: true, value: number }.

For more details, you can see the original pull request on GitHub.

Higher order type inference from generic constructors

In TypeScript 3.4, we improved inference for when generic functions that return functions like so:

  1. function compose<T, U, V>(f: (x: T) => U, g: (y: U) => V): (x: T) => V {
  2. return x => g(f(x));
  3. }

took other generic functions as arguments, like so:

  1. function arrayify<T>(x: T): T[] {
  2. return [x];
  3. }
  4. type Box<U> = { value: U };
  5. function boxify<U>(y: U): Box<U> {
  6. return { value: y };
  7. }
  8. let newFn = compose(arrayify, boxify);

Instead of a relatively useless type like (x: {}) => Box<{}[]>, which older versions of the language would infer, TypeScript 3.4’s inference allows newFn to be generic. Its new type is <T>(x: T) => Box<T[]>.

TypeScript 3.5 generalizes this behavior to work on constructor functions as well.

  1. class Box<T> {
  2. kind: "box";
  3. value: T;
  4. constructor(value: T) {
  5. this.value = value;
  6. }
  7. }
  8. class Bag<U> {
  9. kind: "bag";
  10. value: U;
  11. constructor(value: U) {
  12. this.value = value;
  13. }
  14. }
  15. function composeCtor<T, U, V>(
  16. F: new (x: T) => U,
  17. G: new (y: U) => V
  18. ): (x: T) => V {
  19. return x => new G(new F(x));
  20. }
  21. let f = composeCtor(Box, Bag); // has type '<T>(x: T) => Bag<Box<T>>'
  22. let a = f(1024); // has type 'Bag<Box<number>>'

In addition to compositional patterns like the above, this new inference on generic constructors means that functions that operate on class components in certain UI libraries like React can more correctly operate on generic class components.

  1. type ComponentClass<P> = new (props: P) => Component<P>;
  2. declare class Component<P> {
  3. props: P;
  4. constructor(props: P);
  5. }
  6. declare function myHoc<P>(C: ComponentClass<P>): ComponentClass<P>;
  7. type NestedProps<T> = { foo: number; stuff: T };
  8. declare class GenericComponent<T> extends Component<NestedProps<T>> {}
  9. // type is 'new <T>(props: NestedProps<T>) => Component<NestedProps<T>>'
  10. const GenericComponent2 = myHoc(GenericComponent);

To learn more, check out the original pull request on GitHub.