Skip to content

Variadic tuple assignment fail Β #49138

@craigphicks

Description

@craigphicks

Bug Report

πŸ”Ž Search Terms

variadic tuple bug

πŸ•— Version & Regression Information

The same behavior is visible from 4.2.3 through 4.8dev
although it it as only been a bug since variadic tuples.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// These should be OK since variadic tuples (rest can go anywhere)
// versions 4.2.3 ~ 4.8 dev.
{
    type T12 = ["^", ...(string | boolean | undefined)[], "$"]
    const t12a:T12 = ["^", "$"];        // Should not be an error
    const t12b:T12 = ["^", true, "$"];  // Should not be an error
}

πŸ™ Actual behavior

As shown.

πŸ™‚ Expected behavior

Not errors, because a match exists.

Additional Info

I examined the code. checkArrayLiteral in checker.ts performs a unidirection (left to right) search only,

However, the output from createNormalizedTupleType outputs a normalized type that can have a single tuple in the middle (like the one in the example).

That requires a bidirectional search to get the right results.

Alternatively, createNormalizedTupleType could be forced to roll up everything after the first rest into a single rest. But such an algorithm would result in more false postives (false passes) than necessary. So a bidirectional search is preferable.

I can provide more details if necessary. (I even have bi-directional search code that passes all the baselines tests). Just ask if you want it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions