Skip to content

7.1: array-destructured parameter with a = [] default loses its contextual type (TS7031) #65069

Open

Description

🔎 Search Terms

TS7031, implicit any, array destructuring parameter, default value, contextual typing, padTupleType, 7.1 regression

🕗 Version & Regression Information

⏯ Playground Link

No response

💻 Code

// tsconfig: { "compilerOptions": { "strict": true, "noEmit": true, "types": [] } }
type F = (input?: [string?, number?]) => number;
export const f: F = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0);

The object form from #64974 fails the same way; this report is specifically the array form:

type G = (input?: { a?: string; b?: number }) => number;
export const g: G = ({ a, b } = {}) => (a ? 1 : 0) + (b ?? 0); // also TS7031; tracked in #64974

Without the = [] initializer, the array pattern still checks cleanly on 7.1:

type F2 = (input: [string?, number?]) => number;
export const f2: F2 = ([a, b]) => (a ? 1 : 0) + (b ?? 0); // OK

🙁 Actual behavior

repro.ts(2,23): error TS7031: Binding element 'a' implicitly has an 'any' type.
repro.ts(2,26): error TS7031: Binding element 'b' implicitly has an 'any' type.

🙂 Expected behavior

No errors: a and b take string | undefined and number | undefined from the contextual parameter type, as on 7.0.2.

Additional information about the issue

In checkDeclarationInitializer, parameter array binding patterns always call padTupleType when the initializer type is a tuple. Padding invents any-typed missing elements and overrides the contextual parameter type:

case ast.KindArrayBindingPattern:
    if isTupleType(t) {
        return c.padTupleType(t, name)
    }

#64978 adds a contextual-type guard only for KindObjectBindingPattern / padObjectLiteralType. After that lands, this array case will still fail unless padTupleType gets the same getContextualTypeForVariableLikeDeclaration(root, ContextFlagsNone) == nil guard.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions