index.js
function (expected, expectedIndex, actual, actualIndex) {
   // Check how many optional types in front of this argument that match the current value.
   var consecutiveOptionals = countTrailingOptionals_(expected, expectedIndex, actual[actualIndex]);

   // Check how many required types are behind this argument that match the current value. We
   // will then use this value to determine if the current argument can be allowed to fill an
   // optional spot instead of a required one.
   var matchingRequires = countLeadingMatchingRequires_(expected, expectedIndex,
         actual[actualIndex]);

   // Now that we have found the consecutive matching types, more forward through the arguments
   // to see if there are enough to fill the option types.
   var matchesRequired = 1 + matchingRequires;
   var availableDistance = matchingRequires + consecutiveOptionals;

   // Determine if there are enough optional arguments.
   var i = actualIndex - 1;
   var type = expected[expectedIndex];
   while (i >= 0 && availableDistance > 0 && matchesRequired > 0) {
      if (isOfType(actual[i], type)) {
         matchesRequired--;
      }
      availableDistance--;
      i--;
   }
   return matchesRequired <= 0;
}
function (expected, expectedIndex, actual, actualIndex) {
   // Check how many optional types in front of this argument that match the current value.
   var consecutiveOptionals = countTrailingOptionals_(expected, expectedIndex, actual[actualIndex]);

   // Check how many required types are behind this argument that match the current value. We
   // will then use this value to determine if the current argument can be allowed to fill an
   // optional spot instead of a required one.
   var matchingRequires = countLeadingMatchingRequires_(expected, expectedIndex,
         actual[actualIndex]);

   // Now that we have found the consecutive matching types, more forward through the arguments
   // to see if there are enough to fill the option types.
   var matchesRequired = 1 + matchingRequires;
   var availableDistance = matchingRequires + consecutiveOptionals;

   // Determine if there are enough optional arguments.
   var i = actualIndex - 1;
   var type = expected[expectedIndex];
   while (i >= 0 && availableDistance > 0 && matchesRequired > 0) {
      if (isOfType(actual[i], type)) {
         matchesRequired--;
      }
      availableDistance--;
      i--;
   }
   return matchesRequired <= 0;
}
Checks if the current argument should be left for an optional argument.
@param {Array} expected
@param {number} expectedIndex
@param {?} value
@returns {bool}
@private