Skip to content

Contextual type from destructuring binding for Array#reduce produces bad errors #32140

@RyanCavanaugh

Description

@RyanCavanaugh

TypeScript Version: 3.6

Search Terms: array reduce destructuring binding tuple

Code

const [oops] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);

Expected behavior: Should be OK (oops gets value 1 at runtime)

Actual behavior: It's not good. The empty array in the second reduce argument gets inferred as an empty tuple and things go downhill quickly.

  1. On the call accu.concat(el), we get the error "Type number is not assignable to ConcatArray<never>"
  2. The oops identifier gets errored with "Initializer provides no value for this binding element" because we think the return tuple is empty.

Playground Link: Linky

Related Issues: ?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions