Skip to content

Function signature overload with rest parameter: TS picks the wrong signatureΒ #62019

Closed as not planned
@denis-migdal

Description

@denis-migdal

πŸ”Ž Search Terms

function signature wrong pick

As asked, this is a rewrite of the issue #62000

πŸ•— Version & Regression Information

Tested with the nightly build.

⏯ Playground Link

Playground Link

πŸ’» Code

type         OPTIONS = Record<string, unknown>;
type DEFAULT_OPTIONS = {css: string[]};
type    TEST_OPTIONS = {css: any[]   };

let test: DEFAULT_OPTIONS = {} as TEST_OPTIONS; // TEST_OPTIONS can be cast into DEFAULT_OPTIONS.

/**************************************************************/
{
    function LISS( args: DEFAULT_OPTIONS) : true
    function LISS( args: OPTIONS,
                   ...extensions: unknown[]): false

    function LISS(args: OPTIONS,
                 ...extensions: unknown[]): boolean {
        return {} as boolean
    }

    const a = LISS({} as TEST_OPTIONS); // is false, should be true
    const b = LISS({} as DEFAULT_OPTIONS);
}

πŸ™ Actual behavior

typeof aisfalsewhen it should betrue`: i.e. the second signature overload is chosen.

πŸ™‚ Expected behavior

typeof a should be true: i.e. the first signature overload should be chosen.

Additional information about the issue

As asked, this is a rewrite of the issue #62000

Using [unknown, ...unknown[]] fixes the issue in the example (force the second signature to have at least one extension). But the underlying issue might manifest in other cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions