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. };