Skip to content

Commit eb1b225

Browse files
committed
Pad tuple type initializers of parameter array binding elements
1 parent 4c99084 commit eb1b225

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24233,10 +24233,13 @@ namespace ts {
2423324233
function checkDeclarationInitializer(declaration: HasExpressionInitializer) {
2423424234
const initializer = getEffectiveInitializer(declaration)!;
2423524235
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;
2423624239
const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const ||
2423724240
isDeclarationReadonly(declaration) ||
2423824241
isTypeAssertion(initializer) ||
24239-
isLiteralOfContextualType(type, getContextualType(initializer)) ? type : getWidenedLiteralType(type);
24242+
isLiteralOfContextualType(padded, getContextualType(initializer)) ? padded : getWidenedLiteralType(padded);
2424024243
if (isInJSFile(declaration)) {
2424124244
if (widened.flags & TypeFlags.Nullable) {
2424224245
reportImplicitAny(declaration, anyType);
@@ -24250,6 +24253,22 @@ namespace ts {
2425024253
return widened;
2425124254
}
2425224255

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+
2425324272
function isLiteralOfContextualType(candidateType: Type, contextualType: Type | undefined): boolean {
2425424273
if (contextualType) {
2425524274
if (contextualType.flags & TypeFlags.UnionOrIntersection) {

0 commit comments

Comments
 (0)