Skip to content

isArray type guard regression  #41889

Closed
Closed
@mjbvz

Description

@mjbvz

TypeScript Version: 4.2.0-dev.20201208

Search Terms:

Code

interface IAction {
    x: number;
}

function foo(arg: IAction | ReadonlyArray<IAction>) {
    const actions: ReadonlyArray<IAction> = Array.isArray(arg) ? arg : [arg];
}

Expected behavior:
This compiles fine in TS 4.1.2

Actual behavior:
In the latest nightly, this produces this error:

Type '(IAction & any[]) | (readonly IAction[] & any[]) | (IAction | readonly IAction[])[]' is not assignable to type 'readonly IAction[]'.
  Type '(IAction | readonly IAction[])[]' is not assignable to type 'readonly IAction[]'.
    Type 'IAction | readonly IAction[]' is not assignable to type 'IAction'.
      Property 'x' is missing in type 'readonly IAction[]' but required in type 'IAction'.ts(2322)

Playground Link: https://www.typescriptlang.org/play?ts=4.2.0-dev.20201208#code/JYOwLgpgTgZghgYwgAgJIEEFmAexMgbwChlTkAPALmRAFcBbAI2gG4iBfIomWkLXfDBw4AFHCgBzahn55kAH2QAlCHAAmeADYBPdFChxtAHhnY8APgCUhEmQR4AzmGSIzIB9RXqtu-YZOYbubIALzIegbaAHTADhGGYpLWAPwuksjUANriEgC6bOxAA

Related Issues:

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