Inconsistent behavior of rest tuple with optional elements followed by non-optional elements #51643
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Bug Report
🔎 Search Terms
variadic tuple optional element rest spread inference
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
As pointed out when middle and leading rest elements in tuple types were introduced, optional elements are spread as if the element are non-optional but possibly undefined (1) (2). When a tuple with omitted optional values is spread as a non-trailing rest into a tuple, the values are shifted and don't match the tuple's type.
Related inference behavior was addressed in #39607, but currently a variadic rest tuple with optional elements can be inferred when the rest tuple is followed by non-optional elements. This is incorrect, because the inferred type permits omitted values, resulting in invalid spreads where elements are shifted around.
🙂 Expected behavior
Spreading a tuple with optional elements should result in a type that accounts for potentially omitted optional elements.
Similarly, inferred variadic rest tuples should be restricted to only non-optional elements when followed by non-optional elements, because permitting omitted values results in invalid spreads where elements are shifted around.
The text was updated successfully, but these errors were encountered: