Improved checking for destructuring object literal

TypeScript 1.7 makes checking of destructuring patterns with an object literal or array literal initializers less rigid and more intuitive.

When an object literal is contextually typed by the implied type of an object binding pattern:

  • Properties with default values in the object binding pattern become optional in the object literal.
  • Properties in the object binding pattern that have no match in the object literal are required to have a default value in the object binding pattern and are automatically added to the object literal type.
  • Properties in the object literal that have no match in the object binding pattern are an error.When an array literal is contextually typed by the implied type of an array binding pattern:

  • Elements in the array binding pattern that have no match in the array literal are required to have a default value in the array binding pattern and are automatically added to the array literal type.

Example
  1. // Type of f1 is (arg?: { x?: number, y?: number }) => void
  2. function f1({ x = 0, y = 0 } = {}) { }
  3. // And can be called as:
  4. f1();
  5. f1({});
  6. f1({ x: 1 });
  7. f1({ y: 1 });
  8. f1({ x: 1, y: 1 });
  9. // Type of f2 is (arg?: (x: number, y?: number) => void
  10. function f2({ x, y = 0 } = { x: 0 }) { }
  11. f2();
  12. f2({}); // Error, x not optional
  13. f2({ x: 1 });
  14. f2({ y: 1 }); // Error, x not optional
  15. f2({ x: 1, y: 1 });