Skip to content

Union type conflicts with overloaded function #16816

Closed
@donaldpipowitch

Description

@donaldpipowitch

TypeScript Version: 2.4.1

Code

type Foo = { bar: string };

declare function foos(f: Foo);
declare function foos(f: Foo[]);

function foos2(f: Foo | Foo[]) {
  foos(f);
  // Argument of type 'Foo | Foo[]' is not assignable to parameter of type 'Foo[]'.
  //  Type 'Foo' is not assignable to type 'Foo[]'.
  //    Property 'length' is missing in type 'Foo'.
}

Expected behavior:

foos accepts Foo and Foo[]. foos2 accepts Foo | Foo[]. I think foos2 can just pass its argument to foos without problems.

Actual behavior:

Argument of type 'Foo | Foo[]' is not assignable to parameter of type 'Foo[]'.
  Type 'Foo' is not assignable to type 'Foo[]'.
    Property 'length' is missing in type 'Foo'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions