@@ -24233,10 +24233,13 @@ namespace ts {
24233
24233
function checkDeclarationInitializer(declaration: HasExpressionInitializer) {
24234
24234
const initializer = getEffectiveInitializer(declaration)!;
24235
24235
const type = getTypeOfExpression(initializer, /*cache*/ true);
24236
+ const padded = isParameter(declaration) && declaration.name.kind === SyntaxKind.ArrayBindingPattern &&
24237
+ isTupleType(type) && !type.target.hasRestElement && getTypeReferenceArity(type) < declaration.name.elements.length ?
24238
+ padTupleType(type, declaration.name) : type;
24236
24239
const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const ||
24237
24240
isDeclarationReadonly(declaration) ||
24238
24241
isTypeAssertion(initializer) ||
24239
- isLiteralOfContextualType(type , getContextualType(initializer)) ? type : getWidenedLiteralType(type );
24242
+ isLiteralOfContextualType(padded , getContextualType(initializer)) ? padded : getWidenedLiteralType(padded );
24240
24243
if (isInJSFile(declaration)) {
24241
24244
if (widened.flags & TypeFlags.Nullable) {
24242
24245
reportImplicitAny(declaration, anyType);
@@ -24250,6 +24253,22 @@ namespace ts {
24250
24253
return widened;
24251
24254
}
24252
24255
24256
+ function padTupleType(type: TupleTypeReference, pattern: ArrayBindingPattern) {
24257
+ const patternElements = pattern.elements;
24258
+ const arity = getTypeReferenceArity(type);
24259
+ const elementTypes = arity ? type.typeArguments!.slice() : [];
24260
+ for (let i = arity; i < patternElements.length; i++) {
24261
+ const e = patternElements[i];
24262
+ if (i < patternElements.length - 1 || !(e.kind === SyntaxKind.BindingElement && (<BindingElement>e).dotDotDotToken)) {
24263
+ elementTypes.push(!isOmittedExpression(e) && hasDefaultValue(e) ? getTypeFromBindingElement(e, /*includePatternInType*/ false, /*reportErrors*/ false) : anyType);
24264
+ if (!isOmittedExpression(e) && !hasDefaultValue(e)) {
24265
+ reportImplicitAny(e, anyType);
24266
+ }
24267
+ }
24268
+ }
24269
+ return createTupleType(elementTypes, type.target.minLength, /*hasRestElement*/ false, type.target.readonly);
24270
+ }
24271
+
24253
24272
function isLiteralOfContextualType(candidateType: Type, contextualType: Type | undefined): boolean {
24254
24273
if (contextualType) {
24255
24274
if (contextualType.flags & TypeFlags.UnionOrIntersection) {
0 commit comments