Skip to content

Nested destructuring over a mixed (non-tuple) array literal fails type-checking (const [[m], n] = [[7], 8]) #783

@nickna

Description

@nickna

Summary

Nested array destructuring of a literal whose elements have different types is rejected by the type checker, in both declaration and assignment (#754) forms:

const [[m], n] = [[7], 8];        // Type Error: Index type '0' is not valid for indexing all members of union type 'number[] | number'.
let m2, n2; [[m2], n2] = [[7], 8]; // same

Uniform nesting works: const [[m], [k]] = [[7], [8]] (element type number[]). An explicit tuple annotation does not help (const [[m], n]: [number[], number] = [[7], 8] fails the same way).

Cause

[[7], 8] is inferred as (number[] | number)[] (array, union element) rather than the tuple [number[], number]. The __arrayDestructure normalization (#685) passes arrays through and indexes positionally, so src[0] is the union number[] | number, which is not indexable for the nested [m]. tsc uses the binding pattern as contextual type to infer the rhs as a tuple.

Fix shape

Propagate the destructuring pattern's arity/shape as a contextual type to the initializer so a mixed array literal infers as a tuple (SharpTS already has TryCheckArrayLiteralAsTuple for tuple-context inference — it just is not threaded through __arrayDestructure source checking). Pre-existing; surfaced again by #754.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions